jtorral Posted 14 hours ago Share Posted 14 hours ago I have this simple line of code which somewhat works. Until I try to do math against the $screenWidth $screenWidth = '<script> document.write(screen.width); </script>'; If I echo $screenWidth I see 3072 which is what I expect. But if I do something like $nw = $screenWidth - 20; I get a php error about an Unsupported operand type string - int I tried casting $screenWidth = intval($screenWidth) but I always get 0. So I do some more debugging with this little bit of code echo "<pre>"; print_r(str_split($screenWidth)); echo "</pre>"; echo "<br>"; if( is_numeric($screenWidth) ) { echo "Numeric $screenWidth</br>"; } else { echo "String $screenWidth</br>"; } And here is what I get Array ( [0] => < [1] => s [2] => c [3] => r [4] => i [5] => p [6] => t [7] => > [8] => [9] => d [10] => o [11] => c [12] => u [13] => m [14] => e [15] => n [16] => t [17] => . [18] => w [19] => r [20] => i [21] => t [22] => e [23] => ( [24] => s [25] => c [26] => r [27] => e [28] => e [29] => n [30] => . [31] => w [32] => i [33] => d [34] => t [35] => h [36] => ) [37] => ; [38] => [39] => < [40] => / [41] => s [42] => c [43] => r [44] => i [45] => p [46] => t [47] => > ) String 3072 And var_dump($screenWidth) shows string(48) "3072" I cant tried the ajax method but setting a session is not working as expected. In reality the one line of code is really simple if only I can get the actual numeric value to manipulate in my code. Any thoughts? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/326705-cannot-cast-to-int-when-calling-inline-from-php-returns-string-but-shows-something-else/ Share on other sites More sharing options...
requinix Posted 14 hours ago Share Posted 14 hours ago You cannot run Javascript code inside your PHP code. It does not work and never will work. The only reason you saw "3072" before was because your PHP literally outputted that <script>, the Javascript executed in your browser, and what the code does is literally write the value to the page. The only way to get values like screen.width to PHP is through some sort of request to the server, such as AJAX. If that's not working for you then fix that and then you'll be able to use AJAX just like everyone else. But that's probably the wrong thing. Why does your PHP need to know the screen width? Quote Link to comment https://forums.phpfreaks.com/topic/326705-cannot-cast-to-int-when-calling-inline-from-php-returns-string-but-shows-something-else/#findComment-1649023 Share on other sites More sharing options...
jodunno Posted 7 hours ago Share Posted 7 hours ago 7 hours ago, jtorral said: Until I try to do math against the $screenWidth $screenWidth = '<script> document.write(screen.width); </script>'; I'm not being rude but math involves numbers. <script> document.write(screen.width); </script> - 20 = not valid mathematics. you must realize that you are assigning a string to the $screenwidth variable and not a number. if you use a number then the following code will work: $screenwidth = 3072; echo $screenwidth - 20; As a string, you could echo the string where you want the script to execute within the dom. But then you cannot use the JavaScript to assign the value to a php variable. PHP is a server-side scripting language. JavaScript is a client-side scripting language. I hope that my post helps you understand what is wrong with your logic. Quote Link to comment https://forums.phpfreaks.com/topic/326705-cannot-cast-to-int-when-calling-inline-from-php-returns-string-but-shows-something-else/#findComment-1649037 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.