Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Question Nested if Statement

    Hi guys,

    PHP n00bster here. Why does PHP give me an unexpected "if" statement error when I try to use
    if (empty($errors)) {
    "No items ordered. \n".
    }
    else {
    "Quantity: $quantity_1 Item Number: $item_number_1 \n".
    }

    in the following code snippet? It's part of a form email script that prints the result of a form in an email. I'm trying to check if a variable is empty or not. Am I able to use a nested if statement here or is my syntax just wrong?

    Code:
    	if(empty($errors))
    	{
    		// Send the email.
    		$to = $your_email;
    		$subject = "Canadian Order for UAT Parts from " . $first_name . " " . $middle_name . " " . $last_name;
    		$from = $your_email;
    		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
    		$user_message = stripslashes($user_message);
    		
    		$body = "$first_name $middle_name $last_name from Canada submitted the following UAT Parts order:\n".
    		"Name: $first_name $middle_name $last_name\n".
    		"Email: $visitor_email \n".
    		"Address: $address \n".
    		"City: $city \n".
    		"Province: $province \n".
    if (empty($errors)) {
    		"No items ordered. \n".
    }
    else {
    		"Quantity: $quantity_1 Item Number: $item_number_1 \n".
    }
    		"Message: \n".
    		"$user_message\n".
    		
    		$headers = "From: $from \r\n";
    		$headers .= "Reply-To: $visitor_email \r\n";
    		
    		mail($to, $subject, $body,$headers);
    		
    		header('Location: thank-you.html');
    	}

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Strings cannot contain branches (or most processing instructions actually).
    So this is syntactically invalid:
    PHP Code:
    $str 'this is a string' .
    if (!empty(
    $a))
    {
        
    ' with ' $a ' in it.';

    That requires a concatenation:
    PHP Code:
    $str 'this is a string';
    if (!empty(
    $a))
    {
       
    $str .=  ' with ' $a ' in it.';

    Ternary will work within a concatenated string:
    PHP Code:
    $str 'this is a string' . (!empty($a) ? ' with ' $a ' in it' ''); 
    Is valid.

  • Users who have thanked Fou-Lu for this post:

    JAG (06-16-2011)

  • #3
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thank you, Fou-Lu.

    I actually didn't see I was in the middle of a string when I inserted those if statements. Guess it's all part of the PHP learning curve for me.

    Thanks again.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •