Compare a variable to a number

Hi All

I am sure this is not too difficult to fix and I am sure I have done it before but can’t remember where or when.

I am using some code to get the width of the screen:

<?php $screenWidth = "< script >document.write(screen.width); < /script >"; ?>

NOTE: the script tags are written correctly in my code but would not show in my post without the spaces.

This works and if I <?php echo $screenWidth; ?> it shows the correct screen width.

I then want to use and if else statement:

	if (1290 <= $screenWidth) { $contactColSpanOne = "col-sm-5"; $contactColSpanTwo = "col-sm-3"; }
	else { $contactColSpanOne = "col-sm-4"; $contactColSpanTwo = "col-sm-4"; }

I just cannot get this to work… I have tried many things and spent the past 2 hours trying to work it out.

I am testing this on a screen width on 1920 and $screenWidth returns 1920 and if I change $screenWidth in the if else statement to 1920 then the if else statement works.

I am sure it is just a simple line of code.

Any help would be great.


Try forcing the string to an integer by prefixing $screenWidth with (integer) or (int) I forget which and cannot test on my tablet :frowning:

Thanks… I tried that but may have had the code wrong… an example would be great.

<?php if (1290 <= (int)  $screenWidth) 

Thanks for the example however that didn’t work… any other suggestions?

Unfortunately not :slight_smile:

Upon reflection… I believe the problem is that PHP is a server side language which fails to change the browser JavaScript call.


Try this:

1 Like

You’re right… that is what I got to last time… now I need to remember how I worked around it.

1 Like

I knew I had done this before but couldn’t remember the details… on checking I had put a ticket about this up last year and put the solution in it:

Note that the value is not what the server is outputting. It is outputting the script and the script is writing the value.

For this to work the way I think you have in mind you will need to have JavaScript in the browser send the value back to the server then have PHP send output depending on the value. Or do it entirely with JavaScript.

There are a few ways you could do this, but all have negatives. IMHO it would be a lot easier and much better to simply code the page so it displays OK regardless of screen width and avoid using complicated script hacks.

I would recommend using CSS media queries to optionally show/hide content based on screen width.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.