PHP supports eight primitive types: Boolean, integer, float,
string, array, object, resource, and NULL. Boolean, integer,
float, and string are scalar or simple types. Array and object
are compound types. Resource and NULL are special
types.
Since PHP is a scripting (i.e. loosely typed) language the interpreter
infers the types rather than the programmer declaring the type.
The possible values of a Boolean variable are TRUE and
FALSE.
To aid in programming shortcuts a number of other types are converted
to Boolean values. These include the integer 0 (FALSE),
the empty string (FALSE), the special type NULL (FALSE),
and others.
String literals can be declared with single quotes, double quotes, or
the heredoc syntax. Backslashes are used to escape quotes, as in
other languages. Variables are expanded for strings in double
quotes as in Perl. The heredoc syntax uses <<<
and is useful for multi-line strings. You can get a character in
a string using square brackets, as in an array. For example $str[0]
is the first character in the variable $str. The
function strlen returns the length of a string.
Strings are concatenated with '.' (rather than with '+' in other
languages).
By default, a character in PHP is the same as a byte implying that PHP has no native support for Unicode. However, if you have the Multibyte String extenstion (mbstring) installed, you can set the internal encoding to something else, like UTF-8, using the following code.
The mbstring extension also allows this to be set in the PHP initialization file php.ini.
In PHP arrays are ordered maps. An array can be created with the array function.
This will output the text
You can iterate over an array using either a for
or a foreach statement. Here is a simple for loop.
This produces the following output:
To remove an element from an array use the unset() function. Arrays can
grow and shrink in size in PHP.
A resource is a special type that holds a reference to an external resource, such as a file, a network connection, or a database connection. I show an example of a file type below in the section Working with Files and Text.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.