How to convert String to INT in following situation:?

Asked

Viewed 2,099 times

3

Can you help me understand what’s going on ?

Why after the conversion is it printing ZERO ? It has to do with me populating var with a javascript function to detect the resolution ???

<?php
$largura = "<script type =text/javascript> var largura =  screen.width; document.write(largura); </script>";

echo gettype($largura); //AQUI ELE IMPRIME string
echo $largura; //AQUI ELE IMPRIME 1366 

$largura = (int)$largura;

echo gettype($largura); //AQUI ELE IMPRIME integer
echo $largura; //AQUI ELE IMPRIME 0 

?>
  • 2

    Actually when you do $largura = '<script type="text/javascript"> var largura = screen.width; document.write(largura); </script>'; you are not passing the value "1366" for your variable $largura. You’re assigning all this string for your variable, so the casting (int) returns 0.

  • When you give the echo the code prints out 1366, because the script in the document. Who is printing 1366 is the JS: document.write(largura);.

  • Just replace the variable value $largura in the expression $largura = (int)$largura; to see what you want to convert into INT. Making the replacement we have: $largura = (int)<script type =text/javascript> var largura = screen.width; document.write(largura); </script>; Can’t convert an entire javascript.

3 answers

1


Just replace the variable value $largura in the expression

$largura = (int)$largura;

to see what you want to convert into INT.

Making the replacement we have:

$largura = (int)<script type =text/javascript> var largura = screen.width; document.write(largura); </script>;

Can’t convert an entire javascript.

In fact, the conversion from string to integer depends on the format of the string, so PHP evaluates the format of the string and if it has no numerical value will be converted to 0(zero)

Regarding echo $largura;, replacing the variable value $largura we have:

echo "<script type =text/javascript> var largura =  screen.width; document.write(largura); </script>"

that prints on the page a javascript that in turn prints the variable value largura with document.write.

See how it works here. (does not allow javascript, so it will not run Document.write)

If you want to see the javascript working copy the code below and paste on this page http://phptester.net/

$largura = "<script type =text/javascript> var largura =  screen.width; document.write(largura); </script>";
echo "<br>";
echo gettype($largura). " AQUI ELE IMPRIME string";
echo "<br><br>";
echo "proxima linha é o javaScript veja no código fonte do frame direito";
echo "<br><br>";
echo $largura. " AQUI o JAVASCRIPT IMPRIME a largura";
echo "<br><br>querendo passar para INT um código javascript<br><br>";
$largura = (int)$largura;
echo $largura; 
echo "<br><br>";
echo gettype($largura). ' AQUI ELE IMPRIME integer porque agora $largura = 0';
echo "<br><br>";
echo $largura. ' AQUI ELE IMPRIME 0 porque $largura é 0';
  • Thank you very much my friend, I ended up solving in a simpler way, putting the HTML clauses inside the JS IF because for test purposes it validated straight. - Naimar Nunes now edit

0

I think from the moment you pass the string variable as a type (int), the variable does not recognize the characters inside the string as a numeric value and puts the value 0. I hope I helped

  • Thank you very much my friend, I ended up solving in a simpler way, putting the HTML clauses inside the JS IF because for test purposes it validated straight. - Naimar Nunes now edit

0

It happens because when you do (int)$largura, PHP does not interpret javascript code, it simply takes all the content and tries to turn it into a numeral. Since there is no number to be returned, it returns 0.

The first result only appears the number, because PHP is printing this code on the page and the browser is interpreting the JS code. So the first way works and the second way doesn’t.

Maybe this link will help you. https://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php

  • Thank you my friend, I ended up solving in a simpler way, putting the HTML clauses inside the JS IF because for test purposes it validated right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.