DEV Community

Abdulkareem Mustopha
Abdulkareem Mustopha

Posted on • Edited on

MODULE 2 of #100DaysOfPHP For Absolute Beginners: PHP Building Blocks

I wish to update the module 1 article before moving to module 2, but tight schedule won’t let me do that. This is the second article for the #100DaysofPHP for absolute beginners, our mentor made it as a prerequisite to move to the next stage. We are advised to study some topics and then put them together in an article, which is what made me to be here again with a pleading tone; don’t hesitate to correct me if I err in any part of this broad topics, I am just an absolute beginners!!!

Comparing PHP with other Web Scripting Language

3 main languages comes to my mind: Java, JavaScript and Python.
On Java; it isn't correct to compare Java to PHP. Since PHP is a server-side scripting language whereas Java is a general-purpose language. In other words, PHP is only used as a server-side language where Java is both for server-side and desktop programming language. Moreover, Java is compiled and strongly-typed language. On other hand, PHP is a dynamic typed language.
On JavaScript; it is not fair to compare PHP and JavaScript, as they both have different purposes for web-site development. PHP is a server side scripting language while JavaScript is a client-side scripting language (though JavaScript now supports server side execution via NodeJS). In fact, the most dynamic website is created when we use functions of both these languages together. If PHP is like a paintbrush to paint picture, then JavaScript isa paint-color.
On Python (which is 5years older than PHP); Python and PHP are two of the most popular high-level programming languages. PHP is traditionally used as a server-side scripting language, while Python is valued for its dynamics, availability, and simplicity. Despite their almost identical popularity among developers, these two languages have many specifics and differences.
PHP and Python have a number of common properties that make them popular:
Both languages are easy to learn (compared to C++, Perl, and others);
Both languages are accompanied by the extensive and detailed documentation;
Both languages are open source and can be updated and advanced by users with the purpose of improvement;
Wide and friendly developer communities facilitate the process of learning and working with PHP and Python;
Portability and versatility: both languages have IDEs for all the major operating systems.

The major difference between them is in their syntax and Development Logic; The first and most important difference between PHP and Python is their orientation. Python is aspect-oriented (separates the program functionality and splits the program into separate modules), while PHP is object-oriented (encapsulates code modules that contain certain functions and data into objects).

The list of secondary PHP and Python differences is as follows:
Python is considered a more flexible programming language, while PHP is tightly regulated;
Python uses special packages to load additional libraries, while PHP requires loading them manually;
Unlike PHP, where assuring the software security requires additional tools, applications written in Python are considered among the safest;
Although Python supports a graphical user interface and can be used in web development,
PHP, originally created to support web applications, is more applicable in this area.

HOW TO INSTALL PHP

To start using PHP, you can:
Find a web host with PHP and MySQL support
Install a web server on your own PC, and then install PHP and MySQL

Using a Web Host With PHP Support
If your server has activated support for PHP you do not need to do anything. Just create some .php files, place them in your web directory, and the server will automatically parse them for you. You do not need to compile anything or install any extra tools.
Because PHP is free, most web hosts offer PHP support.

Setting Up PHP on Your Own PC
However, if your server does not support PHP, you must: install a web server, install PHP, install a database, such as MySQL

*The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php

PHP DELIMITERS

PHP code is identified and separated from embedded HTML with delimiters. The delimiters are the <?php and the ?>. All PHP is written between these delimeters. An open delimiter <?php must have a closing delimiter ?>.
Delimiters have a huge role in all the programming languages including PHP. Think of delimiters as a character or set of characters that you use to differentiate between/recognize the different set of similar data.
Also in PHP (in smaller sense) they are used for functions like split or join.
And in the broader sense delimiters are everywhere - whether you have to recognize where your code block starts and ends({,}) and everything else similar which programming languages enforces upon as syntax.

VARIABLE INITIALIZATION IN PHP

Variable is nothing it is just name of the memory location.
A Variable is simply a container i.e used to store both numeric and non-numeric information.

Rules For Variable Declaration

  • Variable name in PHP is case sensitive, $mustbeta is different from $mustBeta. A popular advice is to have a pattern for your variable names, this will aid you when using them later.
  • Variables in PHP starts with a dollar ($) sign, followed by the name of the variable.
  • The variable name must begin with a letter or the underscore character.
  • A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain space

Assigning Values to Variables
Assigning a value to a variable in PHP is quite easy: use the equality(=) symbol, which also to the PHP’s assignment operators. This assign value on the right side of the equation to the variable on the left.
A variable is created the moment you assign a value to it:

PHP Variables Scope
In PHP, variables can be declared anywhere in the script. We declare the variables for a particular scope. There are two types of scope, the local scope where variables are created and accessed inside a function and global scope where variables are created and accessed outside a function.
Example:
<?php
//global scope
$x = 10;
function var_scope ()
{
//local scope
$y =20;
echo "The value of x is : $x " . "
" ;
echo "The value of y is : $y ". "
" ;
}
var_scope ();
echo "The value of x is : $x ". "
" ;
echo "The value of y is : $y " ;
?>
In the above script there are two variables $x and $y and a function var_scope(). $x is a global variable since it is declared outside the function and $y is a local variable as it is created inside the function var_scope(). At the end of the script var_scope() function is called, followed by two echo statements. Lets see the output of the script :
The value of x is :
The value of y is : 20
The value of x is : 10
The value of y is :

There are two echo statements inside var_scope() function. It prints the value of $y as it is the locally declared and cannot prints the value of $x since it is created outside the function.
The next statement of the script prints the value of $x since it is global variable i.e. not created inside any function.
The last echo statement cannot prints the value of $y since it is local variable and it is created inside the function var_scope() function.

Destroying PHP Variables
To destroy a variable, pass the variable to PHP’s unset( ) function.

PHP Data Types

Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Null
  • Boolean
  • Array
  • Object
  • Resource

Note: The PHP var_dump() function (which is used severally in this section) returns the data type and value.

PHP 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. To clarify this look at the example below.

Example:
<?php
$name = "Aphatheology" ;
echo "My in name is $name" . “
echo 'My name is $name' ;
?>

Output:
My name is Aphatheology
My name is $name

PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647 i.e from 2^31 to 2^31.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based - prefixed with 0), or binary (2-based - prefixed with 0b).
If an integer is placed between single or double quotes, it becomes a string!!!

Float (or 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.
If a float is placed between single or double quotes, it becomes a string!!!

NULL

Null is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.

It is a good practice to follow the convention by writing it in capital form i.e NULL but it is not case sensitive.
Variables can also be emptied by setting the value to 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.
If a Boolean value is placed between single or double quotes, it becomes a string!!!

ARRAY

Array is a compound data-type which can store multiple values.
There are two ways to declare an array.

  • An array can be declared using the array() function.
  • An array can be declared wrapping with [ and ]

Elements of the arrays should be separated with a comma. And, elements can be any type of data we’ve discussed in this module.

PHP Arrays Example
<?php
$array = array(false , '573.85' , 'Orange' ,
'Mango' );
var_dump($array);
echo “
”;
$array = [ TRUE , 573.85 , NULL , 'Mango' ];
var_dump($array);
?>
More explanation on Array will be available in subsequent module.

PHP Object Data type

An object is a particular instance of a class where it can be a combination of variables, functions, and data structures.
I will learn/discuss about objects in the Object Oriented Programming class for subsequent modules.

PHP Resource Data type

Another way to assign values to variables: assign by reference. This means that the new variable simply points the original variable. Changes to the new variable affect the original, and vice a verse.

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).
Creating a PHP Constant
The define() function in PHP is used to create a constant as shown below:
define( name, value, case_insensitive )
The parameters are as follows:
name: The name of the constant.
value: The value to be stored in the constant.
case_insensitive: Defines whether a constant is case insensitive. By default this value is False,
i.e., case sensitive.

<?php
// This creates a case-sensitive constant
define( "MODULE2" , "About to complete my article for day 6-10 of my #100DaysofPHP" );
echo MODULE2 . "
" ;
// This creates a case-insensitive constant
define( "module2" , "About to complete my article for day 6-10 of my #100DaysofPHP" , true);
echo MODULE2;
?>

Output:
About to complete my article for day 6-10 of my #100DaysofPHP
About to complete my article for day 6-10 of my #100DaysofPHP
*Constants are Global: * By default, constants are automatically global, and can be used throughout the script, accessible inside and outside of any function.

Constants vs Variables

  • A constant, once defined can never be undefined but a variable can be easily undefined.
  • There is no need to use dollar sign($) before constants during assignment but while declaring variables we use a dollar sign.
  • A constant can only be defined using a define() function and not by any simple assignment.
  • Constants don’t need to follow any variable scoping rules and can be defined anywhere.

PHP Operators

Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition ( +) symbol is an operator that tells PHP to add two variables or values, while the greater than ( > ) symbol is an operator that tells PHP to compare two values.
Below are the various groups of operators and basic explanation of what they do, 2 links is provided after them for further reading.

1. Arithmetic Operators

The arithmetic operators are used to perform simple mathematical operations which are addition, subtraction, multiplication, division, exponential (raise to power) and modulus (remainder of a division).

2. Logical or Relational Operators

These are basically used to operate with conditional statements and expressions. Conditional statements are based on conditions. Also, a condition can either be met or cannot be met so the result of a conditional statement can either be true or false.

3. Comparison Operators

These operators are used to compare two elements and outputs the result in Boolean form. It include operations like 'equal to', not equal to, greater than, less than, identical, not identical and co.

4. Assignment Operators

These operators are used to assign values to different variable, with or without mid-operations. It include operations like 'add and assign', 'subtract and assign', 'divide and assign' and so on.

5. Array Operators

The PHP array operators are used to compare arrays.

6. Increment/Decrement Operators

The increment/decrement operators are used to increment/decrement a variable's value.

7. String Operators

There are two operators which are specifically designed for strings. They are:

  • *Concatenation ( . ) *
    $str1 . $str2
    Concatenation of $str1 and $str2
    Example:
    <?php
    $x = "Hello" ;
    $y = " World!" ;
    echo $x . $y ; // Outputs: Hello World!
    ?>

  • *Concatenation assignment ( .= ) *
    $str1 .= $str2
    Appends the $str2 to the $str1
    Example:
    <?php
    $x = "Hello" ;
    $y = " World!" ;
    $x . = $y ;
    echo $x ; // Outputs: Hello World!
    ?>

For further study on PHP Operators:
Link 1: https://www.tutorialrepublic.com/php-tutorial/php-operators.php
Link 2: https://www.w3schools.com/php/php_operators.asp

The hardest part of this module is the PHP Operators, reading will only give an insight while several practices is what will really let one understand it.
This article is written in fulfilment of Module2 (Day 6 - 10) of our #100daysofPHP for absolute beginners.

PS: I have checked the markdown preference, I still don't know how to format my examples, adding screenshots of them will be too tedious, kindly help a dummy on dev.to

Top comments (0)