Configuration and Installation - PHP Programming: Learn PHP Programming - CRUSH IT IN ONE DAY. Learn It Fast. Learn It Once. Get Coding Today, First Edition(2015)

PHP Programming: Learn PHP Programming - CRUSH IT IN ONE DAY. Learn It Fast. Learn It Once. Get Coding Today, First Edition (2015)

Chapter 3. Configuration and Installation

PHP can be used in three main different fields.

· Desktop applications

· Command Line scripting

· Web applications and websites

To start using the PHP for web applications (server side), you need 3 important things. They are

1. PHP

2. Web Server and

3. Web browser.

The web browser comes along with your operating system. You can rent a web server or get your own. Now, all you need to do is to install PHP on your system. PHP is available for the three most used operating systems which are Windows, MAC OS and Unix.

The installation procedures are different for each operating system. You can follow the instructions given by the PHP service providers and install them on your system. You can download the executable file and install it according to your specifications.

You will need the command line executable if you plan to use the PHP's Command Line scripting capabilities. No browser or server is needed for command line scripting.

Graphical user interface applications for desktop can be coded using the PHP-GTK extension. Though you do not directly use HTML, you'll be using the objects and windows within them. This is different from writing webpages.

Language reference

In PHP, eight primitive data types are supported.

· Boolean: The Boolean data type is the simplest of all. It is used express a truth value i.e., true or false.

· Integer: integers numbers are of the set Z= {.....-3,-2,-1,0,1,2,3,......}. So basically any given number is an integer. They can be specified in binary, octal, decimal and hexadecimal notation with a sign preceding it (- or +). To use an octal notation the number should precede with a 0. To use the hexadecimal notation the number should be preceded with 0X. 0b should be used for a number when using it in binary notation.

Examples for integers

Decimal: 123

Negative: -123

Binary: 0b1100111000

Octal: 0123

· Float: The float data type is one among the primitive data types. It is used to represent rational numbers. Fractions of the type Float are used in applications where large numbers without precise accuracy can be used. Some examples of float are 1.234, 7E-10, 1.2e3 etc.

The size of the float Data type depends on the platform. It can have maximum values of ~1.8e308, which are roughly 14 decimal digits of a normal value. The float dataType has limited precision and also depends on which system it is being used. One should remember not to use float data type if he wants precision and should never equate floating point numbers.

· String: In a programming language, a series of characters which are of the same dataType, is called a string. PHP does not offer the use of Unicode. It only supports the primitive 256 character set. The size of string can be, at max 2 GB. The string data type can be specified in four ways.

1. 1. Single quoted- enclosing the string using single quotes (') is a simple way to specify it. (/) is used to escape a string.

2. 2. Double quoted- for special characters more escape sequences will be interpreted by PHP when double quotes are used to enclose a string. The names will be expanded when double quotes are used to enclose a string. This is an important feature.

3. 3. Heredoc- The Heredoc syntax is the third method to delimit a string.

4. 4. Nowdoc- nowdocs are used for single quoted strings whereas for double coated strings Heredoc is used. The specification of Nowdoc is similar to Heredoc. The only difference is that there is no parsing done inside a Nowdoc. In any static data content, Nowdoc can be used.

· Array: Array in PHP, is an ordered map. Values are associated to keys in the map. For several different uses, this type is optimized. It can be treated as an array, stack, queue, Collection, dictionary, hash table, list etc. An array can have other arrays. With this having multi-dimensional arrays is possible.

Using the array () construct, an array can be created. Example: array(
key => value,
key2 => value2,
key3 => value3,
...
)

· Object: Anything is an object. We should use the new statement, if you need to create a new object.

· Resource: resources are nothing but special variables which hold reference to an external resource. Special functions create and use resources. There are a list of functions which are used for creating or destroying PHP resources. To check if a variable is a resource you can use the function is_resource(). If you wish to get the return type of the resource, you can use the function get_resource_type().

· Null: NULL is a special value which is used to represent available which has no value. The only possible value of the null type is null. You can consider available to be NULL if the constant NULL is assigned to it or when there has no value been set to it yet or when it has been unset().

The Array and Object are compound types while the Resource and Null are special data types.

Constants

An identifier for simple value is called a constant. It cannot be changed during its execution and hence the name, constant. By default, constants are case sensitive. Constant identifiers are always uppercase. As any label in PHP, same rules are followed by Constants. A letter or underscore should be the starting of a name for it to make a valid constant. This can be followed by numbers, letters or underscores.

Here is an example to show you some valid an invalid constant names.

Example:

<?php

// Valid constant names

define("FOO", "something");

define("FOO2", "something else");

define("FOO_BAR", "something more");

//Invalid constant names

define("2FOO", "something");

// This is valid, but best avoided:

//PHP may one day provide a magical constant

// that will break your script

define("__FOO__", "something");

?>

Expressions in PHP

In PHP, expressions are considered to be the most important building stones. Almost everything that you write in PHP is an expression. In a more accurate way we can say that an expression can be defined as "anything which has a value". Constants and variables are most basic form of expressions.

When you give a value "$a=5", you are basically assigning 5 to $a. In other times we can call it as an expression which has the value 5. Now $a equals to 5. If you say that $b= $a, the value of‘a’ will be substituted to b which equals to 5. This is what will happen if everything works correctly.