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>
imagekrazyAsked:
Who is Participating?
 
Ray PaseurCommented:
Here are some ways of handling strings in PHP.

If you use single quotes, PHP will not perform variable substitution inside the string. If you use double quotes, PHP will perform loosely-typed variable substitution inside the string.  The HEREDOC notation is particularly helpful for longer strings (example: web page templates).  Don't be put off by the warning about HEREDOC - just heed it.  If you want to put a quote (single or double) inside a quoted string you can either begin the expression with the other kind of quote, or escape the embedded quote with a backslash.  These are equivalent:

$x = "Ray's";
$y = 'Ray\'s';

If you use an unquoted string, PHP first tries to locate it among the defined constants.  In the event that it's not a defined constant, PHP then issues a message of level E-NOTICE and "pretends" that you meant to use quotes around a literal string.  For better or worse, PHP's default installation suppresses the E-NOTICE messages.  As a result many PHP programmers think it is OK to just skip over the use of quotes because they are not seeing the messages that would alert them to what is going on under the covers.  One day someone may define a constant that matches the name of an array index, and all of a sudden they'll be hit with a run-time error.  You do not want a run-time error!

Don't skip using the quotes.  And don't set the PHP error reporting level to suppress the E-NOTICE messages.  If you do, a simple typographical error can cause a huge debugging exercise, since PHP will not tell you about undefined variables or constants if you have suppressed these messages.

Some places to get a foundation in how PHP works: Make a Google search for Tizag, W3Schools, CodeAcademy.  PHP has its own starting point here:
http://php.net/tut.php

Good books to help you get going:
http://www.sitepoint.com/books/phpmysql5/
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/

A little old and a little more advanced:
http://www.amazon.com/dp/0672328887

Best of luck with your project, ~Ray
0
 
käµfm³d 👽Commented:
Can you share the error?
0
 
käµfm³d 👽Commented:
Also, you didn't quote "steve" in your if.

e.g.

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

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
imagekrazyAuthor Commented:
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!
0
 
käµfm³d 👽Commented:
I suggest reviewing the following documentation for PHP:

http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.constants.php

P.S.

Your if structure is fine. It's the string stuff you need to correct.
0
 
imagekrazyAuthor Commented:
what would you do, in the string sections?
0
 
käµfm³d 👽Commented:
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.
0
 
OmniUnlimitedCommented:
This code will work:

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

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

Open in new window

0
 
Ray PaseurCommented:
<?php // RAY_temp_imagekrazy.php
error_reporting(E_ALL);

// USING QUOTES AROUND VARIABLE VALUES
$s = 'myName';
    
// TESTING TO SEE IF A VARIABLE MATCHES A QUOTED LITERAL STRING
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

0
 
imagekrazyAuthor Commented:
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")
0
 
käµfm³d 👽Commented:
Yes, you can use either quotes in PHP. Just be mindful of the differences that Ray mentioned above.
0
 
imagekrazyAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.