Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php if/elseif else statement

Posted on 2013-05-20
12
Medium Priority
?
501 Views
Last Modified: 2013-05-20
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>
0
Comment
Question by:imagekrazy
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181266
Can you share the error?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181273
Also, you didn't quote "steve" in your if.

e.g.

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

Open in new window

0
 

Author Comment

by:imagekrazy
ID: 39181274
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 39181280
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
 

Author Comment

by:imagekrazy
ID: 39181297
what would you do, in the string sections?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181320
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
 
LVL 17

Assisted Solution

by:OmniUnlimited
OmniUnlimited earned 500 total points
ID: 39181325
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 39181328
<?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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 39181383
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
 

Author Comment

by:imagekrazy
ID: 39181442
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181467
Yes, you can use either quotes in PHP. Just be mindful of the differences that Ray mentioned above.
0
 

Author Closing Comment

by:imagekrazy
ID: 39181478
thank you
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question