Jump to content

cannot cast to int when calling inline <script> from php returns string but shows something else.


Recommended Posts

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

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?

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.