-4

The value of $width is getting echoed fine and tallies with the browser width but the if statement just doesnt execute.

<?php 
$width = "<script>document.write(window.outerWidth); </script>";
echo($width);
if ($width > 599) {
    echo do_shortcode('[codepeople-html5-media-player id="1"]');
} else {?>
    <img src="/wp-content/uploads/2015/04/copy-jandp-logo2.png" alt="logo" /> 
<?php } ?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
MikeKJ
  • 9
  • 3
  • 5
    $width is a string, not a number.... defining a string containing `$width = ""` on a webserver running PHP doesn't automagically execute a javascript function on a web browser and return the result to PHP on the server – Mark Baker Nov 10 '15 at 14:03
  • 4
    Probable dupe: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – John Conde Nov 10 '15 at 14:05
  • all this seems like front-end code, why even use PHP there? it could all be done with JS. – CodeGodie Nov 10 '15 at 14:24
  • you are mixind server side parsing with client-side parsing. maybe do and ajax call which includes your width, parse your shortcode and finally return it and display it. or better use CSS (media query) to hide your unwanted section and display your image – Mujnoi Gyula Tamas Nov 10 '15 at 15:05

3 Answers3

0

As stated in my comment use CSS media query to do your desired effect.

PHP Code:

echo '<div class="codepeople-sc">', do_shortcode('[codepeople-html5-media-player id="1"]'), '</div>';
echo '<div class="codepeople-img"><img src="/wp-content/uploads/2015/04/copy-jandp-logo2.png" alt="logo" /></div>';

CSS Style:

.codepeople-sc{ display: none; }
.codepeople-img{ display: block; }
@media (min-width: 599px){
    .codepeople-sc{ display: block; }
    .codepeople-img{ display: none; }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mujnoi Gyula Tamas
  • 1,759
  • 1
  • 12
  • 13
-3

In your PHP code, $width is a string. So to achieve what you want by changing if ($width > 599) to if ( strlen( $width ) > 599 )

Damilola Olowookere
  • 2,253
  • 2
  • 23
  • 33
  • why would OP want string length? Obviously they are trying to compare the result of `window.outerWidth`, I think the *real* problem is coming from trying to mix JS and PHP – musefan Nov 10 '15 at 14:10
  • if anything you probably want `parseInt` not `strlen`... regardless, the use of PHP in that code is horrible; good example of Spaghetti Code – CodeGodie Nov 10 '15 at 14:27
  • Oops! I didn't "read" the intention of the OP. Apparently, OP wants to know if `$width` is more than `599`, not if length of `$width` is more than `599`. However, @musefan is right: mixing JS with PHP is very bad. In this context, it is rather worse:) – Damilola Olowookere Nov 10 '15 at 14:53
-3

When you assign anything under double quotes to a variable it is a string.
ie: str = "I am a string"; # String
ie: num = 23; # number

When you execute your if statement the $width is a string comparing
to a number, therefore, it will not execute. If you create a string
(ie: x="15") you can cast that to an integer.

The parseInt() function parses a string and returns an integer.
Hope it helps. Good luck!

eof0100
  • 181
  • 4