?
Solved

PHP if statement won't work

Posted on 2009-02-15
7
Medium Priority
?
217 Views
Last Modified: 2012-05-06
This is an easy one, but for some reason it is stumping me.  The java script is working, and I can echo out the $width with no problems.  But, for some reason, when I try to use the $width in a PHP if statement I can't get it to work.  Even when i turn the screen resolution down to 800, and PHP echos out 800, it still prints "Print this on the screen.".  I've done things like this before, but for some reason I can't get this to work. I've got something wrong with my if statement, but I dont' know what.
<?
$width = "<script>document.write(screen.width);</script>";
echo $width;
if ( $width > "1023" ) {	
?>
	<strong> Print this on the screen.</strong>
<?
}
?>

Open in new window

0
Comment
Question by:jbpeake
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:isaackhazi
ID: 23647403
Yes you will have to add an else statement .......


<?
$width = "<script>document.write(screen.width);</script>";
echo $width;
if ( $width > "1023" ) {	
?>
	<strong> Print this on the screen.</strong>
<?
else
}
?>

Open in new window

0
 
LVL 8

Expert Comment

by:isaackhazi
ID: 23647406
What i mean is dont leave the else part balnak , put something in...

Cheers
0
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 23647433
You can't use the > sign when comparing a number to s string. So remove the quotes from the number as in the attached code.
<?
$width = "<script>document.write(screen.width);</script>";
echo $width;
if ( $width > 1023 ) {	
?>
	<strong> Print this on the screen.</strong>
<?
}
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:digital0iced0
ID: 23647646
Thats not the only problem.  You are trying to merge two different languages and it wont work like this.  Remember PHP is server side, and JS is client side.  What your PHP page is doing is outputting a javascript command.  Therefore as far as php is concerned $width is just a string.  PHP will never get the value of the screen like this.  The reason why you get the right number is because the javascript command is the one witting it to the screen not php.  The only way you could do this is to get the width in javascript and send it to a new page as a GET parameter like this newpage.php?width=1023.  Then you can have this php script in the new page and it will work because it will receive it as a parameter.
0
 
LVL 6

Expert Comment

by:digital0iced0
ID: 23647662
In other words the way you're doing it now only the client side (JavaScript) knows the value of the screen width.  You need to let the server (PHP) know the value and you do that through a get or post like I mentioned above.
0
 
LVL 6

Expert Comment

by:digital0iced0
ID: 23647694
Something like this would work:
Page1(Javascript):

<script>
var scrwidth=screen.width;
window.location = "secondpage.php?width="+scrwidth;
</script>

Then in page two (secondpage.php):
<?php
$width=$_GET['width'];
if ( $width > "1023" ) {        
?>
        <strong> Print this on the screen.</strong>
<?
}
?>

0
 
LVL 6

Accepted Solution

by:
digital0iced0 earned 2000 total points
ID: 23647723
Actually a better way would be to have it all in the same page.  Sorry for all the posts I'm half asleep :)

<?php
if(isset($_GET['width'])){
      $width=$_GET['width'];
      if ( $width > "1023" ) {        
      ?>
                  <strong> Print this on the screen.</strong>
      <?
      }
}else{//if its not set then output the javascript to get it
      echo '<script>window.location = "'.$_SERVER['PHP_SELF'].'?width="+screen.width;</script>';
}
?>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question