easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
if
(PHP 4, PHP 5, PHP 7, PHP 8)
El constructor if
es una de las características más
importantes de muchos lenguajes, incluido PHP. Permite la
ejecución condicional de fragmentos de código. PHP dispone de una
estructura if
que es similar a la de C:
if (expr) sentencia
Como se describe en la sección sobre
expresiones , la expresión es evaluada a su
valor booleano. Si la expresión se evalúa como true
,
PHP ejecutará la sentencia y si se evalúa
como false
la ignorará. Más información sobre qué valores evalúan
como false
se puede encontrar en la sección 'Convirtiendo a booleano'.
El siguiente ejemplo mostraría a es mayor que b si $a es mayor que $b:
<?php
if ($a > $b) {
echo "a es mayor que b";
}
?>
A menudo se desea tener más de una sentencia para ser ejecutada
condicionalmente. Por supuesto, no hay necesidad de envolver cada sentencia
con una cláusula if
. En cambio, se pueden agrupar
varias sentencias en un grupo de sentencias. Por ejemplo, este código
mostraría a es mayor que b
si $a es mayor que
$b y entonces asignaría el valor de
$a a $b:
<?php
if ($a > $b) {
echo "a es mayor que b";
$b = $a;
}
?>
Las sentencias if
pueden anidarse dentro de otra sentencias
if
infinitamente, lo cual provee completa
flexibilidad para la ejecución condicional de diferentes partes del
programa.
easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
Normal if example;
if($value["cargostatus"]==0):
echo 'In the Procurement Process';
elseif ($value["cargostatus"]==1):
echo 'In packaging';
elseif ($value["cargostatus"]==2):
echo 'In cargo';
else:
echo 'not specified';
endif;
Multiple comparison operator example in a single line;
($value["cargostatus"] == "0") ? 'In the Procurement Process' : (($value["cargostatus"] == "1") ? 'In packaging
' : (($value["cargostatus"] == "2") ? 'In cargo' : 'not specified'))
What should not be forgotten;
After the first query, a parenthesis will be added to the end of each comparison.
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:
<?php
$v = 1;
$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes'
$r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'
echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed
// and since PHP 5.3
$v = 'My Value';
$r = ($v) ?: 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE
$v = '';
echo ($v) ?: 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE
?>
Parentheses can be left out in all examples above.
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:
<?php
if( $a == 1 || $a == 2 ) {
if( $b == 3 || $b == 4 ) {
if( $c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>
You could just simply do this:
<?php
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) {
//do that something here.
}
?>
Hope this helps!
Any variables defined inside the if block will be available outside the block. Remember that the if doesn't have its own scope.
<?php
$bool = true;
if ($bool) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
It will print 'Hello to all people!'
On the other hand, this will have no output:
<?php
if (false) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php
if ($foo == 3) bar();
?>
do
<?php
if (3 == $foo) bar();
?>
this way, if you forget a =, it will become
<?php
if (3 = $foo) bar();
?>
and PHP will report an error.
In addition to the traditional syntax for if (condition) action;
I am fond of the ternary operator that does the same thing, but with fewer words and code to type:
(condition ? action_if_true: action_if_false;)
example
(x > y? 'Passed the test' : 'Failed the test')
<?php
//Print the day or night message by if command
$t = date("H");
if ($t < "20") {
echo "Have a good day !";
if ($t > "20") {
echo "Have a good Night!";
}
}
?>
You can use this syntax :
if (condition):
// your code
else if:
// your code
else:
// Your code
endif ;
//to use if condition and html together
<?php if(condition) : ?>
// just html code
<?php elseif( condition ): ?>
// just html code
<?php else: ?>
// just html code
<?php endif?>