Solved

php if/elseif else statement

Posted on 2013-05-20
12
466 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Can you share the error?
0
 
LVL 74

Expert Comment

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

e.g.

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

Open in new window

0
 

Author Comment

by:imagekrazy
Comment Utility
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
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
Comment Utility
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
Comment Utility
what would you do, in the string sections?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Assisted Solution

by:OmniUnlimited
OmniUnlimited earned 125 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
<?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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Author Closing Comment

by:imagekrazy
Comment Utility
thank you
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now