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>
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>
Can you share the error?
Also, you didn't quote "steve" in your if.
e.g.
e.g.
if($s=='steve')
{
...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Note the difference between your "myName", "someOtherName", and "steve" and mine.
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!";
}
?>
Note the difference between your "myName", "someOtherName", and "steve" and mine.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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")
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.
ASKER
thank you