mcmuney Posted April 3, 2013 Share Posted April 3, 2013 I'm pulling the resolution by using java and displaying it via php, which works. However, when I want to add/substract, it treats to value ($width) as zero. In the example below, I need "echo $width+1" to = 1281 (currently = 1). How can I make that happen? <?php $width='<SCRIPT language="JavaScript"> <!-- width = screen.width; document.write(width); //--> </SCRIPT>'; echo $width; //this shows resolution, ie 1280 echo $width+1; //this shows 1 ?> Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/ Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 You can't mix and match PHP with Javascript like that. What do you need the width for? It might have to wait until the page has been displayed - you can't get the screen width any earlier than that, but you might be able to do something else that doesn't require having the numbers in front of you. Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/#findComment-1422597 Share on other sites More sharing options...
mcmuney Posted April 3, 2013 Author Share Posted April 3, 2013 I need to use in/then statements using PHP. For example, if($width>1280){ //do X }else{ //do Y. Even though it's displaying the right width, it's not seeing the value as a number; therefore, treating it as a 0. Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/#findComment-1422603 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 When I asked what you needed the width for I wasn't looking for an answer like "to do stuff with it". What is "do X"? What is "do Y"? Are you absolutely positive that you can't do both, conditionally, client-side? Because if not then you have to display a page first, use Javascript to get the screen dimensions, then reload the page passing along the numbers. Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/#findComment-1422695 Share on other sites More sharing options...
Fehnris Posted April 4, 2013 Share Posted April 4, 2013 (edited) Javascript is a clientside language meaning that the code runs when the person loads and views your website. The code runs in the clients browser. PHP is a serverside language meaning that when someone requests the web page the code is executed on the server before the page has even left for the clients browser. What you are trying to do won't work for several reasons. 1) you are using PHP to try to increment the resolution value of the browser The PHP code is executed 'before' it reaches the browser and so the resolution of the browser isn't available to the PHP code. 2) you are trying to increment a variable that isn't a number. The php variable $width doesn't contain a number but instead contains all your Javascript code. Because of this the variable doesn't contain 1280. Your page does as you say display 1280 but this is all done by the Javascript. All your PHP code is doing is effectively outputting your Javascript which is then sent to the browser where is it ran to display the resolution. To accomplish what you need, you should look into writing it all with Javascript. Edited April 4, 2013 by Fehnris Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/#findComment-1422866 Share on other sites More sharing options...
khyzer Posted June 18, 2013 Share Posted June 18, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script > function width() { var width = screen.width; document.getElementById("width").innerHTML="screen width is :" + (width+1); } </script> </head> <body onload="width()"> <div id="width">xbcbcvbc </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/276467-converting-string-to-a-number-for-calculation/#findComment-1436571 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.