0% found this document useful (0 votes)
38 views

Conditionals and Logic in PHP

PHP provides several conditional and logical statements to control program flow. These include if/else statements to execute code based on boolean conditions, comparison operators to evaluate expressions, switch statements for multiple comparisons, and logical operators like AND, OR, and NOT. Nested conditional statements allow for complex decision-making. Truthy and falsy values determine if an expression evaluates to true or false.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Conditionals and Logic in PHP

PHP provides several conditional and logical statements to control program flow. These include if/else statements to execute code based on boolean conditions, comparison operators to evaluate expressions, switch statements for multiple comparisons, and logical operators like AND, OR, and NOT. Nested conditional statements allow for complex decision-making. Truthy and falsy values determine if an expression evaluates to true or false.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Cheatsheets / Learn PHP

Conditionals and Logic in PHP

PHP else statement


A PHP else statement can follow an if block. If the $condition = FALSE;
condition of the if does not evaluate to TRUE , the
if ($condition) {
code block following else will be executed.
// This code block will not execute
} else {
// This code block will execute
}

PHP comparison operators


PHP comparison operators are used to compare two // Comparison operators
values and return TRUE or FALSE depending on the
1 > 3; // FALSE
validity of the comparison. Comparison operators
include: 3 > 1; // TRUE
identical ( === ) 250 >= 250; // TRUE
not identical ( !== )
1 === 1; // TRUE
greater than ( > )
1 === 2; // FALSE
less than ( < )
greater than or equal ( >= ) 1 === "1"; // FALSE
less than or equal ( <= )

PHP If Statements
PHP if statements evaluate a boolean value or if (TRUE){
expression and execute the provided code block if the
echo "TRUE is always true";
expression evaluates to TRUE .
}

$condition1 = TRUE;
if ($condition1) {
// This code block will execute
}

$condition2 = FALSE;
if ($condition2) {
// This code block will not execute
}
PHP switch statement
PHP switch statements provide a clear syntax for a switch ($letter_grade){
series of comparisons in which a value or expression is
case "A":
compared to many possible matches and code blocks are
executed based on the matching case . echo "Terrific";
In PHP, once a matched case is encountered, the code break;
blocks of all subsequent cases (regardless of match) will
case "B":
be executed until a return , break , or the end of the
statement is reached. This is known as fall through. echo "Good";
break;
case "C":
echo "Fair";
break;
case "D":
echo "Needs Improvement";
break;
case "F":
echo "See me!";
break;
default:
echo "Invalid grade";
}

PHP readline()
The PHP built-in readline() function takes a string with echo "\nWhat's your name?\n";
which to prompt the user. It waits for the user to enter
$name = readline(">> "); // receives user
text into the terminal and returns that value as a string.
input
PHP elseif statements
PHP elseif statements must be paired with an if $fav_fruit = "orange";
statement, but many elseif s can be chained from a
single if .
elseif s provide an additional condition to check (and if ($fav_fruit === "banana"){
corresponding code to execute) if the conditional echo "Enjoy the banana!";
statements of the if block and any preceding elseif s } elseif ($fav_fruit === "apple"){
are not met.
echo "Enjoy the apple!";
} elseif ($fav_fruit === "orange"){
echo "Enjoy the orange!";
} else {
echo "Enjoy the fruit!";
}
// Prints: Enjoy the orange!

PHP Truthy and Falsy


PHP values within a condition will always be evaluated to if ("What's going on?"){ // evaluates to
TRUE or FALSE . Values that will evaluate to TRUE
TRUE
are known as truthy and values that evaluate to FALSE
are known as falsy. echo "Let us explain…";
Falsy values include: }
false
// Prints: Let us explain…
0
empty strings
null
undefined
NaN .
All other values are truthy.

PHP Boolean Values


PHP Boolean values are either TRUE or FALSE , // booleans
which are the only members of the boolean type
$is_true = TRUE;
$is_false = FALSE;

echo gettype($is_true);
// Prints: boolean
echo gettype($is_false);
// Prints: boolean
PHP ternary operator
In PHP, the ternary operator allows for a compact syntax // Without ternary
in the case of binary ( if/else ) decisions. It evaluates a
$isClicked = FALSE;
single condition and executes one expression and returns
its value if the condition is met and the second if ($isClicked) {
expression otherwise. $link_color = "purple";
The syntax for the ternary operator looks like the
} else {
following:
condition ? expression1 : expression2 $link_color = "blue";
}
// $link_color = "blue";

// With ternary
$isClicked = FALSE;
$link_color = $isClicked ? "purple" :
"blue";
// $link_color = "blue";

PHP Nested Conditionals


In PHP, nested conditional statements deepen the $num = 5;
complexity of our programs’ decision-making capabilities.
They allow us to create programs where each decision
made sends our program on a different route where it // nested conditional
might encounter additional decisions. if ($num > 0){
echo 'The number is positive. <br>';
if ($num % 2 === 0){
echo 'The number is
even.';
}
} else {
echo 'The number is negative.';
}
PHP Logical Operators
In PHP, expressions that use logical operators evaluate to // Examples of Logical Operators:
boolean values. Logical operators include:
or ( || )
and ( && ) TRUE || TRUE; // Evaluates to: TRUE
exclusive or ( xor ) FALSE || TRUE; // Evaluates to: TRUE
not ( ! )
TRUE && TRUE; // Evaluates to: TRUE
FALSE && TRUE; // Evaluates to: FALSE
!TRUE; // Evaluates to: FALSE
!FALSE; // Evaluates to: TRUE
TRUE xor TRUE; // Evaluates to: FALSE
FALSE xor TRUE; // Evaluates to: TRUE

PHP && Operator


The logical operator && returns: TRUE && TRUE; // Evaluates to: TRUE
TRUE only if both of its operands evaluate to
FALSE && TRUE; // Evaluates to: FALSE
true.
FALSE if either or both of its operands TRUE && FALSE; // Evaluates to: FALSE
evaluate to false. FALSE && FALSE; // Evaluates to: FALSE

$passingGrades = TRUE;
$extracurriculars = TRUE;
if ($passingGrades && $extracurriculars){
echo "You meet the graduation
requirements.";
}
// Prints: You meet the graduation
requirements.

PHP ! (not) Operator


In PHP, the not operator ( ! ) is used to invert a Boolean !TRUE; // Evaluates to: FALSE
value or expression.
!FALSE; // Evaluates to: TRUE

PHP Operator Precedence


Each operator in PHP holds a different operator TRUE || TRUE && FALSE // Evaluates to:
precedence.
TRUE
We can avoid operator precedence confusion by using
parentheses for force the evaluation we want. (TRUE || TRUE) && FALSE // Evaluates to:
FALSE
PHP Xor Operator
In PHP, the logical operator xor stands for exclusive or. TRUE xor TRUE; // Evaluates to: FALSE
It takes two different boolean values or expressions as its
FALSE xor TRUE; // Evaluates to: TRUE
operands and returns a single boolean value.
xor evaluates to TRUE only if either its left operand TRUE xor FALSE; // Evaluates to: TRUE
or its right operand evaluate to TRUE , but not both. FALSE xor FALSE; // Evaluates to: FALSE

Logical Operators - Alternate Syntax


PHP provides an alternate syntax for the || operator — // The or Operator:
the or operator.
TRUE or TRUE; // Evaluates to: TRUE
It also provides an alternate syntax for && operator —
the and operator.
FALSE or TRUE; // Evaluates to: TRUE
These operators have the advantage of making our code TRUE or FALSE; // Evaluates to: TRUE
more human readable.
FALSE or FALSE; // Evaluates to: FALSE

// The and Operator:


TRUE and TRUE; // Evaluates to: TRUE
FALSE and TRUE; // Evaluates to: FALSE
TRUE and FALSE; // Evaluates to: FALSE
FALSE and FALSE; // Evaluates to: FALSE

Multi-File Programs: include


A way to improve our code and separate concerns is with // one.php
modularity, separating a program into distinct,
echo "How are";
manageable chunks where each provides a piece of the
overall functionality. Instead of having an entire program
located in a single file, code is organized into separate // two.php
files.
echo " you?";
In PHP, files can be included in another file with the
keyword include . An include statement is followed by a
string with a path to the file to be included. The code // index.php
from the file will be executed.
echo "Hello! ";
include "one.php";
include "two.php";
// Prints: Hello! How are you?

Print Share

You might also like