PHP building block includes variables, operators, data types, constants and literals.
PHP Compared to Its Contemporaries
PHP is easier to learn and allows for basic things to be done easily plus it is not MVC strict compared to Ruby on rails. On the downside as it is not MVC strict, it doesn't encourage good practice.
PHP Installation
Before you can write and test your PHP scripts, there's one thing you'll need - a server!, because PHP is a server-sided scripting language, you either have to get some web space with a hosting company that supports PHP, or make your computer pretend that it has a server installed. This is because PHP is not run on your PC - it's executed on the server. The results are then sent back to the client PC (your computer).
Apple Users
If you have OS X, then try these sites to get up and running with PHP:
What you're doing here is getting the apache server up and running, so that you can run PHP scripts offline. Pay particular attention to where files are stored, and to the "localhost" address.
Linux Users
There are quite a few sites out there to help Linux users get up and running with the Apache server and PHP. Here are three sites that are worth checking out:
Windows User
Download Xampp, Lampp or Wampp (recommended) and follow the installation process.
PHP Delimiters
PHP delimiters are nothing but the open close tags to enclose PHP script. PHP code can be parsed if and only if it is enclosed with these delimiters.
<?php
//-------code goes here---
?>
Variable Initialization with PHP
A variable consists of a name of your choosing, preceded by a dollar sign ($). Variable names can include letters, numbers, and the underscore character (_). They cannot include spaces. They must begin with a letter or an underscore. The following code defines some legal variables:
<?php
$man;
$a_long_name;
$N123;
?>
PHP Data types
PHP allows eight different types of data types. All of them are discussed below. The first five are called simple data types and the last three are compound data types:
Integer : Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8) or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31.
<?php
$Age = 31;
//31 here is an integer
?>
Double : Can hold numbers containing fractional or decimal part including positive and negative numbers. By default, the variables add a minimum number of decimal places.
<?php
$pi = 3.1428;
?>
String : Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes but it will be treated differently while printing variables.
<?php
$name = "Adekunle";
?>
NULL : These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but its case sensitive.
<?php
$Size = null;
?>
Boolean : Hold only two values, either TRUE or FALSE. Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also consider as false in boolean. If a string is empty then it is also considered as false in boolean data type.
<?php
$Admin = true;
?>
Arrays : Array is a compound data-type which can store multiple values of same data type.
<?php
$odds = array(1, 3, 5, 7, 9);
?>
Objects: Objects are defined as instances of user defined classes that can hold both values and functions. This is an advanced topic and will be discussed in details in further articles.
Resources: Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource.
PHP Constants
When you require a variable that doesn't change through out the script, then you need a constant.You must use PHP's built-in define() function to create a constant. After you have done this, the constant cannot be changed. To use the define() function, you must place the name of the constant and the value you want to give it within the call's parentheses. These values must be separated by a comma
<?php
define("Username", "Alchemist");
?>
PHP Operators
An operator is a symbol or series of symbols that, when used in conjunction with values, performs an action and usually produces a new value.
Types of Operators
Assignment Operator
It consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand:
<?php
$Age = 21;
?>
Concantenation Operator
The concatenation operator is represented by a single period. Treating both operands as strings, it appends the right-hand operand to the left.
<?php
$firstname = "Adekunle";
$lastname = "Afolabi";
name = echo($firstname . $lastname);
?>
Arithmetic Operators
The arithmetic operators do exactly what you would expect?they perform arithmetic operations. The addition operator (+) adds the right operand to the left operand. The subtraction operator (-) subtracts the right-hand operand from the left. The division operator (/) divides the left-hand operand by the right. The multiplication operator (*) multiplies the left-hand operand by the right. The modulus operator (%) returns the remainder of the left operand divided by the right.
Comparison Operators
Comparison operators perform tests on their operands. They return the Boolean value true if the test is successful, or false otherwise.
<?php
// == Represents "equivalent to"
// != Represents "not equivalent to"
// === Represents "identical to"
// !== Represents "not identical to"
// < Represents "less than"
// <= Represents "less than or equal to"
// > Represents "greater than"
// >= Represents "greater than or equal to"
?>
Top comments (0)