Jump to content

Converting string to a number for calculation


mcmuney

Recommended Posts

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
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Fehnris
Link to comment
Share on other sites

  • 2 months later...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.