PHP IF() in Variable Problem

Discussion in 'Web Design and Development' started by JelvisChan, Dec 31, 2009.

  1. JelvisChan macrumors member

    Joined:
    Jan 9, 2009
    #1
    PHP SCRIPTING HELP, PLEASE:

    Does anyone know how to put a PHP if statement in a PHP variable?

    Is it like this?
    EXAMPLE:

    <?php
    $variable = $_POST['name'];
    $if = if ($variable == 'MamaBear'){
    echo 'You are married to PapaBear';
    }
    ?>

    <html>
    <p><b>'.$if.'</b></p>
    </html>



    Gracias!
     
  2. &Ingonyama macrumors member

    Joined:
    May 6, 2008
    #2
    You can't, try:

    PHP:
    if ($variable == 'MammaBear')
    {
        
    $text 'You are married to PapaBear';
    }
    Code:
    <html>
    <p><b><?php echo $text; ?></b></p>
    </html>
    
    You could write it as a ternary statement too:

    PHP:
    <?php
    $variable 
    'MamaBear';
    $text = ($variable == 'MamaBear') ? 'You are married to PapaBear' '';
    echo 
    $text;
    ?>
     
  3. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #3
    Thanks for the reply.
    I am starting to get it going, but I need your help a little more:

    $content='
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>'.$subject.'</title>
    <meta content="text/html; charset=windows-1250">
    <link href="msg/style.css" type="text/css" rel="stylesheet">
    <META HTTP-EQUIV="Expires" CONTENT="-1">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <script language="Javascript" src="msg/javascript.js"><!--
    //-->
    </script>
    </head>
    <body>
    ';
    $content.=$header;


    You have this $content variable that gives an html header, then you start posting things:

    $content.='

    <div align="center"><center>
    <table border="0" width="95%"><tr>
    <td>

    <p align="center"><a href="#new">Post a reply</a> ||
    <hr>
    <p align="center"><b>'.$subject.'</b></p>
    <br>';


    Since you have the html headers, you cannot just start adding PHP, which is what I want to do. I want to post an image, only if the user types in a certain name in the form on the page. I've tried this:

    Before the HTML, I wrote:

    $image = $_POST['name'];
    $echo = ($image == 'Hello') ? ' <img src=\"http://images.macrumors.com/vb/images/statusicon/user_online.gif\">' : ''; }


    and then I wrote below in the HTML:

    echo $echo;

    Would this work?

    Fill free to provide some other scripts.

    Jeff
     
  4. jaikob macrumors 6502

    jaikob

    Joined:
    Jul 1, 2008
    Location:
    Freeland, MI
    #4
    Or you could do this:

    PHP:
    <?php
    $if 
    NULL;
    $variable $_POST['name'];

    if(
    $variable == 'MamaBear') {
    $if "You are married to PapaBear";
    }

    ?>
     
  5. Stampyhead macrumors 68020

    Stampyhead

    Joined:
    Sep 3, 2004
    Location:
    London, UK
    #5
    Be careful using PHP function words as variable names. This could cause errors to come up. Best to stay away from using words like 'if', 'echo', etc. as variables.
     
  6. OutThere macrumors 603

    OutThere

    Joined:
    Dec 19, 2002
    Location:
    NYC
    #6
    You can incorporate segments of php anywhere within your html...so you could do something like this:

    PHP:
    <html>
    <head>
    <title>
    Foo
    </title>
    </head>
    <body>
    This is my page!
    <br>
    <?php
    if ($_POST['name'] == "Bob") {
    echo 
    "<img src=\"./bob.jpg\"><br>Hi Bob!";
    }
    ?>
    </body>
    </html>
    This would check if the user had put the name Bob in the name form on the previous page, and display a picture and say hi if they had.

    You should probably read a php tutorial as it looks like you're kind of starting from scratch. I used the tizag one (http://www.tizag.com/phpT/) when I was learning.
     

Share This Page