I can't compare two variable - problem with data type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jothish
Forum Newbie
Posts: 1
Joined: Mon Jun 07, 2010 1:20 am

I can't compare two variable - problem with data type

Post by jothish »

Hi Friends,

I want to write a program to find out the screen resolutions and if the screen resolution is not equal to 1024*768, I need to show an error.
So by using below script I will get the W and H of screen resolutions.
But I can't compare that. Please see the code I used.

Code: Select all

<?php
$w = '<script  language="JavaScript">document.write(Number(screen.width));</script>';
$h = '<script  language="JavaScript">document.write(Number(screen.height));</script>';
			
if($w<1024){
  echo "Please set your screen resolution greeter than 1024x768";
}
?>
But It is not working.
if I print " var_dump($w) ", it shows as its data type is "string",
So I tried to user (int) to convert type and settype($w,'integer');
But I still not able to compare.
Can you help me to solve this problem.
regards
Jothish
Last edited by Benjamin on Mon Jun 07, 2010 7:45 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: I can't compare two variable - problem with data type

Post by markusn00b »

You must understand the separation between PHP and JavaScript.

When you make a request to an HTTP server, if a type handler matches the file type to one specified as being PHP, the server invokes the PHP module to parse and return the page. PHP sends its data to buffers that your server then use to give data to the browser. At this point it is important to realise PHP has left the building. Once your text has appeared on screen, you do not have a connection to PHP anymore.

JavaScript, if it's not running as the server-side programming language -- which is possible now -- will be running as a client-side, in-browser programming language. Considering that PHP does not run in the browser, as mentioned before, it runs in the server, your JavaScript cannot communicate with PHP (unless you use AJAX or WebSockets).

In fact, you do not need PHP here at all. Simple just send the JavaScript to the browser in an HTML file and it will be run properly.

Mark.
Post Reply