PHP Building Blocks
Mikoyoarts
Posted on June 8, 2019
PHP and other Web Scripting Languages:
Installation of PHP:
In order to develop and run web pages driven by PHP the following softwares are very needful:
i- A Web Server: PHP needs a web server software to be able to execute script which are stored on the server. A number of different web servers exists but the most used is the Apache Server which is open source. The Apache Server can be Downloaded from https://httpd.apache.org/download.cgi.
ii- A Database: PHP uses a database for storing files that are being used on any webpage of a given website. A number of databases exist which is very compatible with the PHP scripting language but the most used is My SQL and it is also has an open source version which can be downloaded from https://www.mysql.com/downloads/.
iii- A PHP Parser: The PHP Parser takes a web developer's PHP scripts which has been executed and generates the HTML output which can be sent and displayed on web browsers.
All the above listed softwares must be downloaded and configured to work with each other before you can start writing and executing PHP scripts on your Computer System. But in recent years, just as technology advances, there has been a release of various software bundles which comprises of all three softwares and the only thing needed is to download them and install and automatically the entire configuration is done during the installation process of these software bundle. Examples of these softwares bundles include:
- XAMPP: Cross Platform, Apache, MySQL, PHP and Pearl.
- MAMP: Apple Mac, Apache, MySQL, and PHP.
- LAMP: Linux, Apache, MySQL and PHP.
PHP Delimiters:
Delimiters are set of characters used to differentiate the different segments of a written code.
Some examples of PHP delimiters are listed below:
i- PHP open and close tag delimiters (<?php ...?>), PHP short tag (<?...?>), PHP print statement (<?=..;?>)e.t.c. These PHP delimiters enables the identification of PHP scripts.
ii- PHP Alphanumeric delimiters such as forward slashes (/), hash signs (#), comma (,), fullstop (.), tildes (~) e.t.c. These PHP alphanumeric delimiters can be used to split up a string or even convert an array into a string and they also perform many other functions.Variable initialization with PHP:
Variables are used as "containers" in which we store information. Variables help us create a storage location on the memory of our computer system. The value of variables can be changed as the program code sequence goes on. Just like any other programming language variables must first be initialized before they can be used in PHP. PHP variable initialization is as follows:
A PHP variable starts with a dollar sign ($), which is followed by the name of the variable. Example: $variable_name = value;
Rules for naming a PHP Variable:
- A variable name must start with a letter or an underscore
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($name and $NAME would be two different variables)
Example using a PHP Scenario:
<?php
$name = 'John';
$figure = 25;
?>
- PHP Data types: The following are datatypes supported by PHP:
- String, this is a sequence of characters contained within quotes, like "Hello world!"
- Integer, these are whole numbers, without a decimal point, like 4195.
- Float, these are floating-point numbers, like 3.14159 or 49.1.
- Boolean,this have only two possible values either true or false.
- Array, these are named and indexed collections of other values.
- Object, these are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
- NULL, this is a special type that only has one value: NULL.
Resource, these are special variables that hold references to resources external to PHP (such as database connections).
PHP Constants:
Constants are similar to variables except that they cannot be changed or undefined after they've been defined.
Begin the name of your constant with a letter or an underscore.
To create a constant, use the define() function:
Example: define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant;
value: Specifies the value of the constant;
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false;
example below creates a constant with a case-sensitive name:
<?php
define("MSG", "Hi SoloLearners!");
echo MSG;
// Outputs "Hi SoloLearners!"
?>
- PHP Operators:
PHP operators are divided into the following categories:
I. Arithmetic Operators
- Addition,
- Subtraction,
- Multiplication, / Division, % Modulus (to get the Remainder in a division) ** Exponentiation (to raise to the power)
II. Increment & Decrement
The increment operators are used to
increment a variable's value.
Types of increment operators
$x++; // post-increment
++$x; // pre-incrementThe decrement operators are used to
decrement a variable's value.
Types of decrement operators
$x--; // post-decrement
--$x; // pre-decrement
III. Comparison Operators
IV. Logical (or Relational) Operators
V. Assignment Operators
VI. Conditional (or ternary) Operators
Posted on June 8, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.