0

I would like to present some php with the help of html formatting.

My purpose is to print:

Life status: dead (1850 in New-York)

or (if not dead):

Life status: alive

The place and date should only by printed if the condition $life_status is set to "dead".

The following code works for the dead situation but not for the alive one.

<h3>Title</h3>
<ul>
    <li>Life status: <?php echo $life_status;?>
    <?php if($life_status="dead"):?>
        (<?php echo $date_death; ?> in <?php echo $place_death; ?>).
       </li>
    <?php endif; ?>
</ul>

For the alive sitation, I obtain:

Life status: Alive ( in ).

How can I applied the php condition on "( in)."?

1
  • 1
    You are opening your <li> tag but only closing it if your if evaluates to TRUE. You should move the </li> to after your endif; so that the tag always closes. Commented Feb 9, 2017 at 20:11

3 Answers 3

2

Your operator for checking if $life_status is dead is wrong. It is a single equal and should be double. Code below reflects these changes and implements what @AmericanUmlaut correctly suggested in the comment above to make your html actually valid.

<h3>Title</h3>
<ul>
    <li>Life status: <?php echo $life_status;?>
    <?php if($life_status == "dead"):?>  // This maybe the issue, wrong operator used to evaluate.
        (<?php echo $date_death; ?> in <?php echo $place_death; ?>).
    <?php endif; ?>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

You are using the wrong operator. = is assignment, == tests equality.

<h3>Title</h3>
<ul>
    <li>Life status: <?php echo $life_status;?>
    <?php if($life_status=="dead"):?> // == here instead of =
        (<?php echo $date_death; ?> in <?php echo $place_death; ?>).
    <?php endif; ?>
    </li>
</ul>

(Note that I also moved the outside of the if block to address the issue I noted in my comment.)

2 Comments

Thanks. I did the modifications. However, with two =, the condition is never activated. I obtain Life status: alive or Life status: dead only. It is strange because echo $life_status prints "dead" or "alive". Any idea?
Ok I fix it. The "dead" was a link and therefire didn't activate the condition.
-1

In Php strings are not compared directly and may not give the results which are intended. Therefore, using strcmp(); function will do the work. It returns 0 when 2 strings compared are equal and is a case sensitive function.

In your case:

<h3>Title</h3>
<ul>
    <li>
        Life status: <?php 
            echo ( !strcmp($life_status,"dead") ) ? 'Dead ( '.$date_death.' in '.$place_death.' ).' : 'Alive' ;
       ?>
    </li>
</ul>

I have used the Php Ternary operator known as Shorthand if/else. The ! will reverse the 0 to 1 if its true to show the Dead output and else will show the Alive output.

Php Ternary : http://php.net/manual/en/language.operators.comparison.php

Php strcmp() : http://php.net/manual/en/function.strcmp.php

I hope it helps :)

Regards, SA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.