PHP default argument values

If you don’t pass any value to the function, it will use default argument value.
PHP default argument values is an example of passing arguments to function.

For example:


In above example, we don’t pass any value to function family().
But when it is call it will use default value.
As shown in first family() function call, it take 5 as value for $members.
But in second family() function call, it use value of $members is 2 because it is pass by function.


PHP allows the use of special type NULL as default values.

For example:




Also PHP allows use of arrays as default values.
You can not use a variable, a class member or a function call as default value.
The default value must be a constant expression.

If you are going to use one default argument and another is non-default argument.
Then make sure any defaults should be on the right side of any non-default arguments.
Otherwise, things will not work as expected.

For example:


This above example won’t work as expected. Therefore the correct code is shown in below example:


Help others by sharing the content!

PHP Array

PHP array is an variable which stores the multiple values of related data in a single variable.


Creating an PHP array

array() function is used to create an array.
The key can either be an integer or a string. The value can be of any type.
It takes any number of comma-separated key => value pairs as arguments.


Types of array


For example:


  • An array index can be any string value, even a value that is also a value in the array.
    The value of array[“foo”] is “bar”.
    The value of array[“bar”] is “foo”
  • The comma after the last array element is optional.
    This is usually done when you want to single-line array like array(1,2).
    It is preferred than writing array(1,2,).
    But for multi-line array is is commonly used because it is easier to add new element at the end of array.
    In previous of PHP 5.4 you have to use array() like this, but as of PHP5.4, array() is replace with array[].
    For example:
  • The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
    For example:

  • You can specify only key for some elements and leave others.
    For example:


    Here, you can see from output the last value “d” was assigned the key 4.
    This is because PHP increment the previous integer key i.e. 3.


Help others by sharing the content!

PHP Arguments

PHP Function arguments

Information may be passed to functions via the argument list.
PHP arguments are specified inside a parenthesis after function_name.
The PHP arguments are evaluated from left to right.
You can give multiple number of arguments as you want by separating them with comma(,).
PHP supports passing arguments by value, passing by reference, and default argument values.


Passing PHP arguments by value


In above example function sum takes two arguments $a, $b.
When function called the $a is assigned by 10 and $b will assigned by 20.
Then it will perform summation and output addition.

When function sum call second time then it will perform addition of $a = 2 and $b = 4.


Passing arguments by reference

Lets see an example,

As you can see in above example, a value of $a is not change outside the function.
Hence to allow a function to modify its arguments, they must be passed by reference.

The ampersand(&) is use before argument name in function definition to pass arguments by reference.

In this example, a variable $str1 is passed to the function con().
The value of str1 is concatenate with the value of str2.

Help others by sharing the content!

PHP goto

The PHP goto operator can be used to jump to another section in the program.
It some time there are multiple number of loops, and you have to exit and goto some specified loop.
Then you can use multiple break. But this is so inonimous.
Therefore in place of multi-level break, goto is use.
PHP goto is followed by target label.
Target label is a section where has to jump.
Target label must be within the same file and context.
You cannot jump out of a function or method.





Help others by sharing the content!

PHP continue

PHP continue is a keyword use within looping structures.
continue skip the current loop iteration and then begin the next iteration depending on condition.
continue also accepts numeric argument like continue 1, continue 2 etc.
The default value is 1, thus skipping to the end of the current loop.
From PHP 5.4.0, continue 0; is no longer valid.
In previous versions it was interpreted the same as continue 1.




Here it skip the value 3 due to continue.

PHP continue with arguments


Here when $j = 2, it skip ends execution of ‘if’ and inner ‘for’ loop.

Help others by sharing the content!

PHP break

PHP break is a keyword use with mostly for, do-while, while and switch structure.
PHP break ends execution of current loop.
break keyword can accept numeric arguments. By default value is 1. It will enclose only one immediate structure.
For example:
break 1 or break – It ends execution of one loop.
break 2 – It ends execution of 2 loops.


Note: From PHP5.4.0, break 0; is no longer valid.
In previous versions it was interpreted the same as break 1;



In above example you can see only one value of a is print because of break.
break ends the current loop.
Due to break only one time condition is checked and one time loop is executed.
If there will not any break then loop will execute 10 times.


PHP break with switch structure:

We see the use of break in switch structure chapter.



break with arguments


Now understand how break work in switch structure.
There are 2 loops, outer loop while and inner loop switch.
At beginning, at $a = 0; both while and switch condition is true.
Therefore it print statements of case having matching case value i.e. Number 0.
But after matching case 0, there is no break so it print next statements upto break.
AS you can see in output when $a = 0, it print Number 0, Number 1, Number 2.
break 1 results termination of switch and increment $a by 1.
In this way, when $a = 1 and $a = 2, it will print respected matching case statements.
When $a = 3, only “Number 3” is print and it break the execution of both switch and while loop due to break argument 2.

Help others by sharing the content!

PHP Loop

Loops are use to execute block of statements repeatedly as long as the while expression evaluates to TRUE.
PHP loop are used to execute the same block of code a specified number of times. PHP supports following four loop types.

PHP supports 3 type of loops:

  1. while loop
  2. do-while loop
  3. for loop
  4. foreach

WE are discussing all above loops in next chapters with there syntax, flowchart, and also examples.

There are some terms use with loops that are breal and continue.
We will discuss about continue and break keywords used to control the loops execution.

Help others by sharing the content!

PHP Control Structure

In normal program execution is done in order in which they have been written.
But sometimes we have to change path of execution of code and this is done by using control structure.
PHP Control structure is block of code that decides the execution path of a program .

PHP script is series of statements. These statements can be variable, expression, functions call, loop, assignment or control statements. Sometimes the group of statements form statement-group within curly braces to perform some particular task.

As in many languages there is need of control statements like if, if-else, switch etc.
In PHP there also following important control structures:

We explain each control structure in next chapters in detail with flowchart, example and syntax of each.

Help others by sharing the content!

PHP Data Types

A PHP data types is the classification of data into a category according to its attributes.
PHP is a loosely typed language, the type of a variable is explicitly set by programmer.
PHP determines the data type at runtime depending on attributes of data supplied.
var_dump is function returns the data type and value of variable.

PHP supports 10 primitive data types that categorized in 3 types:

  1. Scalar types
  2. Compound types
  3. Special types

php data types

Scalar PHP data types

PHP supports following 4 scalar data types:

  • integer
  • float(floating-point number, aka double)
  • string
  • boolean

Compound types

PHP supports following 4 compound data types:

  • array
  • object
  • callable
  • iterable

Special types

PHP supports following 2 special data types:

  • resource
  • NULL

Now following is a brief description of each data type with examples.


Integer is number of the set {…,-3,-2,-1,0,1,2,3….}.
Integer can be specified in:

  • binary
  • decimal
  • hexadecimal
  • octal

Binary integer literals are available since PHP 5.4.0.
To use number as hexadecimal(base 16) prefix number with 0x.
To use number as octal(base 8) prefix number with 0.
For using number as binary(base 2) prefix number with 0b.


PHP does not support unsigned integers.
The size of integer is platform-dependent.
A constant PHP_INT_SIZE is used to determine the integer size.
The constant PHP_INT_MAX is used to determine the maximum value.
The constant PHP_INT_MIN is used to determine the minimum value.




Floating-point numbers are decimal numbers. They are also known as double or real numbers.
The size of float is platform dependent.



String is a series of characters.
A string literals can be specified within any single or double quotes.



This is a simplest data type.
Boolean data type returns true or false.
True or false are case-insensitive.
Boolean data type is used to test conditions.
‘==’ is operator use to test condition which returns boolean.



Arrays give you the ability to store not just one value inside a variable, but a whole bunch of values in a single variable.
Following example shows how to declare array data type.

We will discuss array in detail in our separate chapter array of this tutorial.


NULL value represents a variable with no value.
A variable is considered to be null if it is assigned the constant NULL or it has not set any value.

Help others by sharing the content!

PHP Constants

PHP constants is an identifier (name) for a simple value.
As the name suggests, that value cannot change during the execution of the script.

Rules for constants

  • The rules for naming constant name is same as rules for naming variable names.
  • $ sign is not used with constants.
  • Constants are case-sensitive.
  • A constant name must start with letter(a-z or A-Z) or underscore(_) followed by any number of letters, numbers or underscores.
  • Constant name cannot start with numbers.
  • The scope of constant is global means you can access constants anywhere in script.


Syntax to create php constant:

Following is a general syntax to create constant:

define() – It is a function use to define constant.
constant_name – For convention constant_name are always uppercase.
constant_value – must to specifies.
true/false specifies whether the constant name should be case-insensitive.
If you do not mention anything then it is by default false.



Constants are case-sensitive.

Let’s see following two examples to understand,



Difference between variables and constants

  • To create variable, you have to use doller($) sign before variable name.
    But there is no need to write a dollar sign ($) before a constant
  • Variables have there scoping rules, local, global, static.
    Constants accessed anywhere in script without regard to scoping rules.
  • You can change value of variable at anywhere in program.
    But Constants can not be redefined.
  • Value of variable is simply assign using assignment operator(=).
    Constants cannot be defined by simple assignment, they defined using the define() function.
Help others by sharing the content!