If Block Inside Echo Statement

if block inside echo statement?

You will want to use the a ternary operator which acts as a shortened IF/Else statement:

echo '<option value="'.$value.'" '.(($value=='United States')?'selected="selected"':"").'>'.$value.'</option>';

How to embed if statement inside echo

Everything is php so need to use more than the first <?php Finish each echo before checking with if. Like this:

<?php 
echo '<td><select id="depuis" name="depuis">
<option value="4"';
if(isset($_POST['depuis']) && $_POST['depuis'] == '4') {
echo ' selected';
}
echo ' >Something here maybe?</option>...etc

PHP how to put an if statement inside an echo block

Don't.

Ignoring for a moment that outputting your HTML embedded inside echo statements like this is an anti-pattern (consider using a templating engine!), you will make your code really hard to read by mixing your logic and your content even more, which you will be doing if you employ the conditional operator* here.

In fact, in general, if you have anything more than basic string interpolation in your output statements, you're not separating your logic and content enough.

Here, it will look something like this:

$users = get_users();
$i = 0;
// Array of WP_User objects.
foreach ( $users as $user ) {
echo "<option value='select-$i' ";
if (isset($prfx_stored_meta['wedding-user']))
echo selected($prfx_stored_meta['wedding-user'][0], 'select-one');
echo ">" . esc_html( $user->display_name ) . "</option>";

$i++;
}

See how messy it is? And with a long line using the conditional operator it would be even worse!

* Not the "ternary operator"! Being "ternary" is its arity, not its name.

If else inside echo in php

You can try using ternary operator like this:

( ($tijz >= 0) ? gmdate("H:i:s", $tijz) : "Time's Up!" )

Then:

echo "
td class='inhoud_c' width='5%'>".$i."</td>
<td class='inhoud' width='25%'><a href='profile.php?x=".$naam."'>".$naam."</td>
<td class='inhoud_c' width='50%'>
<table border='0' cellspacing='0' style='margin: 0px;'>
<tr>
<td>
<img src='".$icon."' alt='' border='0'>
</td>
<td>
".$land."
</td>
</tr>
</table>
</td>
<td class='inhoud_c' width='20%'>
". ( ($tijz >= 0) ? gmdate("H:i:s", $tijz) : "Time's Up!" ) ."
</td>
</tr> ";
$i++;

How to include an if-statement inside a PHP echo?

You can temporarily define a variable to store your output.

$output = '<table class="table table-bordered table-condensed table-datatable table-hover">
<tbody>

<tr>
<td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td>
</tr>
<tr>
<td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
</tr>';
if(condition)
{
$output .= ' <tr>
<td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is</strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>';
}
$output .= '<tr>
<td style="height: 5px;" width="100%"> </td>
</tr>

<tr>
<td style="height: 5px;" width="100%"> </td>
</tr>

</tbody>
</table>';

How to use if and else inside echo statement?

Yes the ternary operator is what you're looking for, but you have to chain them to get the if-else statement.

echo '<div>'.(LoginCheck($this->db) == true ?
'<span class="option1">' : '<span class="option2"></span>').'</div>'

The evaluation statement comes before the question mark (?) and there is no if tag to put before the statement. It's just the expression to evaluate to true/false, and then the question mark, and then the first statement is if the expression is truthy, the second statement, separated from the first by a colon (:), is if the expression is not truthy.

Look here for some more info.

If and else statement in PHP inside of an echo

Your code may not have been properly formed. Try this instead:

    <?php

if( isset($_GET['id_user']) ){
include("conexion.php");
$rep = $db->query( "select * from user WHERE id_user=" . $_GET['id_user'] );

// YOU ARE FETCHING A SINGLE USER... NO NEED FOR A LOOP...
// JUST GET THE ARRAY OBJECT AND USE IT DIRECTLY...
$l = $rep->fetch();
?>

<form class= mainSettingsForm add method='POST'>
<label>Numero utlisateur :</label>
<input disabled='disabled' type='text' name='ref' value='<?php echo $l[0]; ?>'/>
<input name='id_user2' type='hidden' value='<?php echo $l[0]; ?>'/>

<label>Nom utlisateur :</label>
<input type='text' name='username2' value='<?php echo $l[1]; ?>' />

<label>Nom :</label>
<input type='text' name='nom2' value='<?php echo $l[3]; ?>' />

<label>Prenom :</label>
<input type='text' name='prenom2' value='<?php echo $l[4]; ?>' />

<label>Statut :</label>
<select name=statut >
<option value='active' <?php if($l[6] == "active"){ echo "selected";} ?> >active</option>
<option value='inactive' <?php if($l[6] == "inactive"){ echo "selected";} ?>inactive</option>
</select>

<input class=btn-primary type='submit' name='enregistrer' value='enregistrer' >
</form>

<?php } ?>

How to put condition inside the echo statement?

You are not escaping ' properly, try this :

while($row = $result->fetch_assoc()){
echo '<tr>
<!--Photos first-->
<td><center>';
if(!empty(trim($row['photo']))){
echo "<img src='" . $row['photo'] . "' class='img-circle' height='100px' width='100px'> ";
echo '<br>' .$row["username"]. '<br></td>';
}
}

Use echo inside php one line if statement

As you only mentioned in your question that print is a function but print is not a function but it has some return value thats why it can be used in expressions also but on the other hand echo doesnot have any return value.

also keep in mind that Ternary operator returns a value.

"In C-alike languages there is a distinction between statements and expressions. Syntactically, echo is a (simple) statement, like break or return and print is an (unary) operator, like "!" or "~". Therefore, like any other statement, echo cannot be part of an expression"

PHP treat echo as a statement.whatever you will write that will be displayed as it is.

you can read the detailed explanation here Reference: Comparing PHP's print and echo



Related Topics



Leave a reply



Submit