Expressions in PHP

Expressions in PHP
 
Expressions in PHP
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


Expression - is the cornerstone of PHP. Almost everything you write in PHP is an expression. Expressions are the "bricks" that make up any PHP-programs. An expression in PHP is everything with a value. Conversely, if something has a value, that something is an expression.

The basic forms of expressions are constants and variables. For example, if you write "$sum = 60", you assign "60" to the variable $sum:

$sum = 60;

In the example above $sum - is a variable, = - is assignment operator, and 60 - is the expression. Its value is 60.

It's possible for the expression to be a variable, if it is assigned a specific value:

$a = 19;
$b = $a;

The first line of this example the expression is the constant 19, in the second line - the expression is the variable $a, because previously it was assigned 19. And, therefore, $b = $a is also an expression.

Slightly more complex example of expressions are functions. For example, consider the following function:

function simpleFunc()
{
  return 41;
}

Based on the fact that you are familiar with the concept of functions, you'd assume that $x = simpleFunc() is essentially equal to writing $x = 41, and you're right. Function - is an expression whose value is the something that the function returns. Since simpleFunc() returns 41, the value of the expression "simpleFunc()" is 41. Typically, the function does not return a static value but calculated.

PHP supports three types of scalar values: integer, floating point and string values (scalar values are values that you can not "break" into smaller pieces, unlike, for example, arrays). PHP also supports two composite (non-scalar) types: arrays and objects. Each of these types of values can be assigned to a variable or returned as a function.

PHP - is an expression-oriented language and almost everything is an expression. Consider the example we've already dealt with: "$a = 19". It is easy to notice that there are two values present - the value of the integer constant "19" and the value of $a, which at the same time has value 19. But in fact, here is another value - the assignment value itself. The assignment is calculated into the assigned value, in this case - into 19. In practice, this means that "$a = 19", no matter what it does, is an expression with the value 19. Thus, the expression "$b = ($a = 19)" is equal to "$a = 19; $b = 19;" (the semicolon here marks the end of the expression) Since the assignments are analyzed form right to left, you can also write "$a = $b = 19".

Logical expressions

Logical expressions - are expressions that can have only two values: true or false (which is almost the same as 0 and 1). However, speaking more strictly, any expression can be considered logical in the "logical" context (for example, the construction of if-else). After all, TRUE can be any non-zero number, non-empty string, and so on, and FALSE is just everything else.

For logical expressions all properties of logical variables are valid. These expressions are most likely to occur when using the operators >, <, and == (equal), | | (or), && (and),! (NOT), and others. For example:

$x = 15 < 7; // $x = false
$x = $y == 2; // $x = true
$x = $y >= 2 && $y <= 12; // $x = true, if $y in the range of 2 to 12
$x = !($y || $z) && $h; // $x = true, if $y and $z are false, and $h - true

How to check if the logical variable is true of false? Exactly the same as any logical expression:

$y = $x >= 2 && $x <= 12; // assign $y value of a logical expression
if($y) echo 'x is in the right range of values';

String expressions

Strings in PHP are some of the most basic objects. They can include text which contains formatting characters and binary data.

Defining a string in quotes or apostrophes can begin from one line and end - on the other. Here is an example of a string expression:

$str = 'Just text';

 

Read also

What is PHP?

What is PHP?

Classes and objects in PHP

Classes and objects in PHP

How to access classes and objects in PHP

How to access classes and objects in PHP

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation