PHP Magic Constants

PHP Predefined Constants

PHP has nine predefined constants that change value depending on where they are used, also called the “magic constants”.

These magic constants are written with a double underscore at the start and the end, except for the ClassName::class constant.

Continue reading PHP Magic Constants

PHP Constants

Constants are like variables, except that once they are defined they cannot be changed or undefined.

PHP Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Note: Unlike variables, constants are automatically global across the entire script.

Continue reading PHP Constants

PHP Math

PHP has a set of math functions that allows you to perform mathematical tasks on numbers.

PHP pi() Function

The pi() function returns the value of PI:

Example

echo(pi());

Continue reading PHP Math

PHP Casting

Sometimes you need to change a variable from one data type into another, and sometimes you want a variable to have a specific data type. This can be done with casting.

Change Data Type

Casting in PHP is done with these statements:

  • (string) – Converts to data type String
  • (int) – Converts to data type Integer
  • (float) – Converts to data type Float
  • (bool) – Converts to data type Boolean
  • (array) – Converts to data type Array
  • (object) – Converts to data type Object
  • (unset) – Converts to data type NULL

Continue reading PHP Casting

PHP Numbers

In this chapter we will look in depth into Integers, Floats, and Number Strings.

PHP Numbers

There are three main numeric types in PHP:

  • Integer
  • Float
  • Number Strings

In addition, PHP has two more data types used for numbers:

  • Infinity
  • NaN

Variables of numeric types are created when you assign a value to them:

Example

$a = 5;
$b = 5.34;
$c = "25";

Continue reading PHP Numbers

PHP – Escape Characters

Escape Character

To insert characters that are illegal in a string, use an escape character.

An escape character is a backslash \ followed by the character you want to insert.

An example of an illegal character is a double quote inside a string that is surrounded by double quotes: Continue reading PHP – Escape Characters

PHP – Slicing Strings

Slicing

You can return a range of characters by using the substr() function.

Specify the start index and the number of characters you want to return.

Example

Start the slice at index 6 and end the slice 5 positions later:

$x = "Hello World!";
echo substr($x, 6, 5);

Note The first character has index 0.

Continue reading PHP – Slicing Strings

PHP – Concatenate Strings

String Concatenation

To concatenate, or combine, two strings you can use the . operator:

Example

$x = "Hello";
$y = "World";
$z = $x . $y;
echo $z;

The result of the example above is HelloWorld, without a space between the two words. Continue reading PHP – Concatenate Strings

PHP – Modify Strings

PHP has a set of built-in functions that you can use to modify strings.

Upper Case

Example

The strtoupper() function returns the string in upper case:

$x = "Hello World!";
echo strtoupper($x);

Continue reading PHP – Modify Strings

PHP Strings

A string is a sequence of characters, like “Hello world!”.

Strings

Strings in PHP are surrounded by either double quotation marks, or single quotation marks. Continue reading PHP Strings