Echo if else statement inside a echo? Is this possible?
Results 1 to 3 of 3

Thread: Echo if else statement inside a echo? Is this possible?

  1. #1

    Echo if else statement inside a echo? Is this possible?

    I have a page that uses phpmailer() to send emails. Works great. Does everything I need. And I need to do a if else statement inside the echo of $body

    This is the code I'd like to run. By itself it works fine
    PHP Code:
    <?php

    $url 
    "http://www.example.com/problem_pic.jpg";

    $file_headers = @get_headers($url);
    if(
    $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    echo 
    '';
    } else {
    echo 
    "Here is a picture of the problem<br>";
    echo 
    "<td><a href='$url'>Problem Pic</a></td>";
    }
    ?>
    But I wanted it used within a echo

    Example of the code of what I'm doing with phpmailer

    PHP Code:
    $mail->AddAddress "user@somedomain.com";
    $mail->Body "
    <html>
    <body>
    Hello 
    $user,<br>
    Here is some information on the problem<br><br>
    $problem<br>
    ##Here is where I need the if else statement to insert the picture if there is one
    <br>
    If you have any questions please let me know<br>
    Thanks,<br>
    Admin"

    I can't figure out how to get that if else statement to work inside my echo.

    Is there anything I'm missing? Or am I over thinking this?

    Let me know

    TIA

    Sean

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    You can use the ".=" operator to append text to a string variable, e.g.:
    PHP Code:
    $foo "This is a test.";
    if(
    == 1) {
       
    $foo .= " It is only a test.";
    }
    else {
       
    $foo .= " Just kidding.";

    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  3. #3
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    Firstly, do you really want to echo this information or do you want to add it to your mail message? Using echo won't add any additional information to your mail message.

    Second, you could use your if statement to assign the appropriate data to a variable and then just echo the variable:
    PHP Code:
     <?php

    $url 
    "http://www.example.com/problem_pic.jpg";

    $file_headers = @get_headers($url);
    if(
    $file_headers[0] == 'HTTP/1.1 404 Not Found') {
      
    $text_to_echo "";
    } else {
      
    $text_to_echo "Here is a picture of the problem<br>";
      
    $text_to_echo .= "<td><a href='$url'>Problem Pic</a></td>";
    }

    echo 
    $text_to_echo;
    ?>
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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