Jump to content

[NEWBIE ALERT] If..else within a switch

- - - - -

  • Please log in to reply
3 replies to this topic

#1
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Whats better than learning PHP? Eating PIZZA and watching the SUPERBOWL while learning PHP!

Alright alright, so I'm trying to work with switch. I want to output a particular statement based on the first/last name inputted. Currently, any "Lucas" first name (with any last name) will output the desired output for only a "Lucas Cul". It's also giving me this error when I try to add the else statement:

Parse error: syntax error, unexpected T_ELSE in 

switch($fname)		{


case "Daniel":

	if ($lname = "Mosq")	{

			echo "Welcome, righteous Supervisor!";

								}

	break;


case "Lucas":

	if ($lname = "Cul")	{

			echo "Welcome, leader of the apes!";

	else	{

		echo "You are not the Lucas we are looking for.  Access denied.";

			}

							}

	break;

	

default:

	echo "Your name is not recognized, however we will give you temporary access.";

					}

Thanks for the advice,

Cheers

#2
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
I saw a syntax issue with the { and } and fixed it, but it's still not recognizing the else condition.

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
if ($lname = "Mosq") {

A single "=" is assignment. It's not often an assignment operator fails making it always true.

A double equals "==" converts to the same data type and compares.

A trip equals "===" keeps the same data type and compares. They need to be the same data type to be true.

You need the second one for this to work properly. I did some formatting for you.
<?PHP
$fname = "Lucas";
$lname = "Smith";
switch($fname) {
    case "Daniel":
        if ($lname == "Mosq") {
            echo "Welcome, righteous Supervisor!";
        }
        break;
    case "Lucas":
        if ($lname == "Cul") {
            echo "Welcome, leader of the apes!";
        } else {
            echo "You are not the Lucas we are looking for.  Access denied.";
        }
        break;
    default:
        echo "Your name is not recognized, however we will give you temporary access.";
} 


#4
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi Blaine,

Thanks for the response. I've changed my code to mimmick yours - and am now getting a parse error:

switch ($fname)			{

		case "Lucas":	

			if ($lname == "Cul");	{

				echo ("Welcome, psycho!");

				} else {

				echo ("You are not the Lucas we are looking for.  Access denied.");

				}

				break;

Quote

Parse error: syntax error, unexpected T_ELSE in

Usually I'd look for too many brackets, but they seem to all match up, less the one following the switch statement (which is paired down at the end). The unexpected T_ELSE is referencing the first } else { line.

/sad panda

[EDIT: DISREGARD. I HAD A ; AFTER EACH IF STATEMENT --> CAUSED A FUSS. PROBLEM FIXED, PAGE WORKS FINE NOW! THANKS FOR ALL THE HELP! :)]




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users