Solved

php if/elseif else statement

Posted on 2013-05-20
12
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 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 125 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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 110

Accepted Solution

by:
Ray Paseur earned 250 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to customize the text in the legend in highcharts 3 36
Hide and Unhide Table 6 27
Two decimal 5 30
Group By Function Required in PDF Output 21 38
This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to count occurrences of each item in an array.

730 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