Link to home
Start Free TrialLog in
Avatar of imagekrazy
imagekrazy

asked on

php if/elseif else statement

Hi, i am practicing the if/elseif/else statement, in php it is causing an error.

here is my code;

<!DOCTYPE html>
<html>
    <head>
            <title>php else if statement</title>
      </head>
      <body>

    <?php
    $s=myName;
    if($s==steve)
    {
        echo "you are the Master of the universe";
}
elseif($s==someOtherName)
{
    echo "you suck!";
}
else{
    echo "You rock!";
}
   
    ?>
    </body>
</html>
Avatar of kaufmed
kaufmed
Flag of United States of America image

Can you share the error?
Also, you didn't quote "steve" in your if.

e.g.

if($s=='steve')
{
...

Open in new window

Avatar of imagekrazy
imagekrazy

ASKER

Use of undefined constant myName - assumed 'myName' (line 8)Use of undefined constant steve - assumed 'steve' (line 9)Use of undefined constant someOtherName - assumed 'someOtherName' (line 13)You rock!
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what would you do, in the string sections?
You're trying to use strings literally--that is, without quoting them. In many programming languages, strings must be surrounded with quotes. Depending on the language you would use either single or double quotes--PHP happens to support either. The compiler is trying to tell you that you are not properly quoting your strings. You need to quote each string literal in your code.

e.g.

<?php
	$s='myName';

	if($s=='steve')
	{
		echo "you are the Master of the universe";
	}
	elseif($s=='someOtherName')
	{
		echo "you suck!";
	}
	else
	{
		echo "You rock!";
	}
?>

Open in new window


Note the difference between your "myName", "someOtherName", and "steve" and mine.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So all of these are right? the single quotes and the double quotes? and the

Variable can be both of these also;
  $s= 'myName'; if( $s == 'steve')

  $s="steve";  if($s=="steve")
Yes, you can use either quotes in PHP. Just be mindful of the differences that Ray mentioned above.
thank you