1

I have what should be a very simple question. I am using the following simple PHP if/else statement. When nomobile is not defined or not equal to "true", then the javascript shown below should execute. If it is equal to true, then it should not execute. However, the javascript is executing even when nombobile=true. There is probably a minor typo that is messing everything up, but I am driving myself crazy trying to find what is wrong and can't figure it out.

<?php
  if ($_GET["nomobile"]!="true")
    {echo '
    <script type="text/javascript">
    if (screen.width <= 699) {
    document.location = "http://www.heliohost.org/m/";
    }
    </script>';}
  else {} ?>
6
  • 4
    Do you mean that it is a string, set to "true", or a boolean with a value of true? Commented Aug 7, 2012 at 18:12
  • 1
    What does var_dump($_GET['nonmobile']) produce when added above the if statement? Commented Aug 7, 2012 at 18:14
  • I'm not very experienced with PHP, but I think a string. It should be so that if you go to heliohost.org/?nomobile=true then it does not redirect to /m/ Commented Aug 7, 2012 at 18:15
  • @andrewsi How exactly would you pass in a $_GET variable set to any other type than a string? Commented Aug 7, 2012 at 18:16
  • 1
    @Christopher Are you sure the screen width is actually a number less than or equal to 699? Have you logged it somewhere? Because I see the javascript in the source when ?nomobile=true is tacked onto the end of the URL, and the javascript isn't there when ?nomobile=true isn't there. Also, using Firebug, I set a breakpoint on the conditional and it did fire. Check your values. Commented Aug 7, 2012 at 18:27

4 Answers 4

2

Could it be that you're not checking if the $_GET value is set? Taking this statement of yours and converting it into code should produce this:

When nomobile is not defined or not equal to "true"

if (!isset($_GET["nomobile"]) || $_GET["nomobile"] != "true"){ ... }

There might be issues happening if it's not set.

I hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1

I think the problem is with your Javascript. Verify that screen.width evaluates to a number that is less than or equal to 699 by alerting it (or logging it or however you choose to do it).

To clarify, when I try to browse to http://www.heliohost.org/?nomobile=true, I see the Javascript in your code. If I try to browse to http://www.heliohost.org/ or http://www.heliohost.org/?nomobile=foo, the Javscript isn't there. Which tells me that your Javascript isn't doing what you expect it to, not that there's a problem with your PHP code.

5 Comments

The problem is with the PHP being executed when it shouldn't be. The Javascript isn't causing this
@PriestVallon According to my tests, the PHP code is executing just fine and injecting the javascript when it's supposed to. Did you test your claim or are you just being argumentative?
I'm just saying what the question claimed, that the If/Else was the problem and not the JS. Not meant to be argumentative.
@PriestVallon I think this question is a good example of The XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem - and that's why I answered the way I did.
I fully agree. The question asker not replying or explain things doesn't help!
0
if(!isset($_GET['nomobile']) || (isset($_GET['nomobile']) && $_GET['nomobile'] != "true")){
    echo '<script type="text/javascript">
    if (screen.width <= 699) {
        document.location = "http://www.heliohost.org/m/";
    }
    </script>';
}

2 Comments

Why call isset() twice? if (isset($_GET['nomobile']) && $_GET['nomobile'] != 'true') would suffice.
Ah, it's just the english language converted to code. If it doesn't exist or if it is not "true" when it exists.
-2

Remove Double quotes.

<?php
    if ($_GET["nomobile"]!=true) {
        echo '<script type="text/javascript"> if (screen.width <= 699) {  document.location = "http://www.heliohost.org/m/"; } </script>';
    }
    else {}
?>

1 Comment

This is not the solution, as it's equivalent to the code in the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.