0

Im having trouble trying to get this to work.

 <?php

   $_SESSION['age'] = 11

        if($_SESSION['age'] < 14) {
            echo "<h2>Your only $_SESSION['age']? Thats Young!</h2>";
        }
    ?>
0

1 Answer 1

5

If you want to echo PHP variable inside echo function, you must as first break echo with ", then add a "merge char" ., then your variable and then again "merge char" and ".

Also don't forget to add semicolon to end of your line which contains definition of $_SESSION['age'].

Edit your code to:

 <?php

   $_SESSION['age'] = 11;

        if($_SESSION['age'] < 14) {
            echo "<h2>Your only ".$_SESSION['age']."? Thats Young!</h2>";
        }
?>
Sign up to request clarification or add additional context in comments.

8 Comments

$_SESSION['age'] = 11 syntax error
add ; to end of your line with $_SESSION['age'] = 11 (edited in my code)
good, also mention one more point, make sure you are using session_start(); otherwise it will not work.
if you are using session variables, shouldn't you be GETTING the $_SESSION['age'] variable instead of SETTING it? (for example $age = $_SESSION['age']); Otherwise there is no benefit in referencing the $_session variables.
Also, this wasn't my full code. I'm making a quick game using html forms and php. The age is defined elsewhere on a different page :P. I just added the "$_SESSION['age' = 11;" for reference.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.