The previous part of PHP Programming Language: introduction. This PHP Basic syntax, I was explained about the life cycle, data type, define a variable, and operator in PHP programming language. Not only that, there are some other things also included in PHP Programming language. In this part, I will explain plot, array, and function.
Plot in the PHP
The plot is syntax that uses for define program plot. There is two plot of PHP programming language:
Conditional is used to determined which code to run. Usually conditional start with checking some parameter to be selected. In conditional, there is some syntax we can use. For the first is conditional if statement. There are three methods to write the code if statement.
- Some code to execute if one condition is true
- some code that executes if a condition is true and another condition is false. Here is sample code.
- some code that executes if condition with more than two conditions. Here is sample code.
Second conditional is switch statement. The switch statement will select one of many blocks of code to be executed.
Looping is a condition that we do a looping the code program. While building a website or web application. We are often with cases that require us to repeat certain statement. PHP provide code that we can use to repeat statements including for, do, while, do-while, and foreach. Here is an example of code:
Array in PHP
an array is a collection of data stored in a place (variable) simultaneously and accessible by using its index. Array in PHP programming language consist of 2 type:
Array with numeric index
This type array has an index of numeric type sorted from the smallest to the largest. Usually, array index starts from zero (0).
$car = array(“toyota”,”honda”,”bmw”,”kia”);
the result of that code: toyota honda kia
This type of array with an in an index that we can define with a particular name.
$employee = array(“CEO” => “john”, “secretary” => ”kate”, “supervisor “ => ”chris”);
the result of that code: john chris kate
PHP have some function to sorting the array. Here is the function:
- sort() – sorting array value in ascending order
- rsort() – sorting array value in descending order
- asort() – sorting associative arrays in ascending order, according to the array value
- ksort() – sorting associative arrays in ascending order, according to ro the array key or index
- arsort() – sorting associative arrays in descending order, according to the array value
- krsort() – sorting associative arrays in ascending order, according to ro the array key or index
Function in PHP
A function is a program block defined by a particular name and used to perform a specific task. The various types of functions are as follows:
Functions that do not return a value
This function only executes the existing script on the function block.
Function with parameter
This function allows us to enter information (parameter) to be processed inside the function. Example:
Function that returns a value
This function will return a value from a variable.