Functions in PHP

A function is a block of code that can be used many times in a program.

It helps us to save a lot of codes and time because function defines only one time and we can use it multiple time whenever & wherever we want.

The most important reason for using function is ‘Reusability’.

Consider an example:
In your program you need a square value of any number more than one time, then what will you do?

You re-type the logic of square value of different number every time.


But using the function you need to declare logic of square of any number only one time and use it anywhere any time without re-typing same code for your program.

PHP User Defined Functions

There are thousands of built-in functions in PHP.

We can also create our own functions in PHP.

PHP function separates the programming logic & easy to understand the flow of code.

How to Define a Function in PHP

A function is a block of statement defined by the developer that can be used repeatedly in a program.

During execution of a program, a function does not execute automatically, the function can be executed upon function call.


The syntax of a function in PHP is as below

NoteFunction name always starts with letter or _ (underscore), never start with integer or any other special characters.

If the function name is too long separate two words by using _ but don’t use space between two words.

PHP is a case-sensitive language so function names are also case sensitive.

Most of the developer follows camel case rules to define function name. Like functionName() not functionname()

Example

Output

Explanation

By typing ‘showMsg()’code you can easily display ‘Hello World’ message anywhere in your program without typing ‘Hello World’ repeatedly.

Passing Function Arguments or Parameters in PHP

The function can take user input as an argument or parameter and return value. The function always returns type.

Function with Arguments or Parameters

Output

Explanation

$num is an argument or parameter which received the number value like 43, 65, 112, or 225.

We can use more than one arguments or parameters by using comma separator.

Using squareOfAnynumbers () you can easily calculate square value of any number in your program.

Example

Function with multiple Arguments or Parameters

Output

Call by value in PHP

PHP allows you to call the function by value. Let’s understand the concept of “call by value” with examples.

Function Call By Value:

Output

Explanation
Call by value means argument ’s value directly passed to the function and the value only modified into the function, not outside of the function.

So, $a only changed into doSomeThing() while passed inside the function, that’s why doSomeThing($a) returns 4 but $a is still 3.

Swap two numbers using Call By Value:

Output

Explanation
$a and $b changed within function so when we call the function swapTwoNumbers($a,$b).

It returns 8 6 means $a = 8 and $b = 6
But outside of the function $a still 6 and $b still 8.

Call By Reference in PHP function

PHP allows function call by reference also. Let’s start to understand with the simple example.

Function Call By Reference:

Output

Explanation

Call by reference means value and address (memory location) both passed to function and value will be modified within the function and also outside of the function.

Here we used & (ampersand) as the reference of argument’s address in the function definition.

So any modification of variable in the function also reflects outside of the function in the original variable.

So, $a return 4 and doSomeThing($a) also return 4.

Swap two numbers using Call By Reference:

Output

Explanation
$a and $b changed within function so when we call the function swapTwoNumbers($a,$b) it returns 8 6
means $a = 8 and $b = 6
and outside of the function $a 8 and $b 6.

Recursion in PHP

In programming, language Recursion occurs when function continues to call itself using some sort of condition.

If there is no condition defined, function calls occur infinite times. These sort of conditions are called the base case.

If a base case is reached function returns the value and stop the recursion.

Until the base case requirement fulfills function automatically calls itself to continue the recursion.

The syntax of the recursive function:

Factorial using the recursive function:

Output

Explanation
Frist base case is $num is less than 0 means $num value is -1 or any negative number then recursion will be stopped and return -1.


The second base case is $num is equal to 0. If $num value is 0 then recursion will be stopped and return 1.


Otherwise fact() automatically called by itself and return the factorial of any positive number.


Here we passed 6 as input, so the base case has not reached then recursion continued for 5th step and returned 720.

PHP array types

Mainly two types of Array in PHP

  1. SINGLE ARRAY/ SINGLE DIMATIONAL ARRAY
  2. MULTIPLE ARRAY/ MULTI-DIMAINTIONAL ARRAY
  3. This two types of the array divided into two types of INDEX ARRAY/ NUMERIC ARRAY & ASSOCIATIVE ARRAY.

    Single Array
    In the single array, we can store multiple values in one variable.

    Example

    Like a fruits array store all fruits name.
    All previous examples are single array.

    Numeric Array / Index Array

    In the numeric array, all index value must be integer or number. If you only store data in an array, index automatically starts with zero this kind of array is called numeric or index array. You don’t need to assign index.
    But if you want to address the index value you can, the index must be assigned with integer type of value.

    Example

    Associative Array

    If you assign index value with string and each element can be assigned by unique key this is called Associative array.

    Above example is an associative array.

    Benefit of this array is we can easily recognize the details of the employee or anything (which you store).

    We can also store these details in numeric array

    But you don’t know which value is the name of this employee, which value is the address of this employee and also same as age and designation.

    But in the associative array, we easily find the employee name by its index value which declared by name.
    print_r(), var_dump() and foreach loop used to print an associative array.

    Output

    Multi-dimensional Array or Multiple Array

    In multiple array we can store more than one array in a single variable. So we can manage more than one array in a single stack.
    Like we can store all employee details in a single variable

    Output

    The main array ‘employees’ have 5 index each index store a single array.
    This is also two dimentional array because you can return any employee details by two indexes.
    Like if we want to display the second employee’s designation just print

    Result

    In this way you can create multi-dimensional arrays and store multiple array in one single variable.

Categories php

Array in PHP

An array is a data structure or a collection of data that contains a group of elements under same name.

Typically these elements are all of the same data types or different data types.

for example:
If a complex has thirty flats each flat having same features then the whole complex is considered to be an Array.

Each of thirty flat is an element of that array and as each flat shares the same type of features – those are considered as same data types.

So, in a programming language like PHP, the array is a collection of different type of value in a single variable.

You also can say a string is one kind of array because string consists of multiple characters.

Note: In programming languages like ‘C’, only one type of value (integer or string or float etc.) can be stored in an array and must be defined at the beginning of the program, but in PHP we can store different types of value (string, integer, Boolean, null, object, float) in a single array.

Define an array


An array is a data structure that stores one or more different type of values in a single variable.

Syntax of an array

Note: Most of developers use syntax type II for robust programming support.

How to print array in PHP

How to print an array in PHP?

Answer is very simple declare and initialize array use print_r() function to print array.

array elements are stored in continuous memory location

Note: this two functions (print_r() and var_dump()) are also debugging function in PHP (about debugging function will be discussed later)

Output:

now 0 1 2 3 are index of the value means 0 1 2 3 in this position apple mango banana lemon are stored respectively.

Index means which is used to identify the particular one value/element in the array.

Explanation:
We can find a house by its address but when we try to find a room in an apartment there must be a number or floor division which separates every room/house from each other rooms/houses.

In similar way multiple value stores in single array separates by a unique index number.

It is also called an ‘ARRAY IDENTIFIRE’. Index, always starts from 0 and automatically incremented, but you also set index manually.

Example:
You can return particular one element of any array by using index number of this array.

In another way you also define array in PHP:

Display an array

This is the example of manual index value. We can also use in this manner.

How to know the length or size of an unknown array

Output

Printing array using loop in PHP

Result

Using FOREACH loop

Result

How to store values in array in php using for loop

How to store values in array in php using forEach loop

How to store values in array in php using while loop

Output of all above is same

Output

HTML del tag

HTML del tag is used to represent set of characters deleted from the document. The characters inside the del tag are represented as text with centre line(text with strukethrough line).

Syntax:

Example:

Result: