The cart is empty

Conditionals in uPHP

Conditionals

Every programming language requires some form of conditional operator. The if operator in uPHP has the following syntax:

if (condition) {
  // your code if true
} else {
  // optional code if false
}

Note: Single if statements can be executed without curly braces. However, all else statements need to be enclosed in curly braces. The following code snippet illustrates different comparisons:

$x = 3;

// use == notation to compare without assigning.
if ($x == 3)	// this is a single line statement and does not require braces
  print("x is 3");
if ($x = 4) // this will always return true since x is assigned the value 4
  print("x is 4");

A more complex example shows the use of Boolean comparisons:

if (($x > 1) && ($x <=5)) || ($x == 7)) {
  print("x in range");
} else {
  print("x out of range");
}

Here, if x has a value between 2 and 5, or 7 then the expression will evaluate to true.

Comments | Add yours
  • No comments found
Add comment