Category Archives: Core Php

PHP Functions


PHP Functions:– In addition to the built-in functions, PHP also allows you to define your own functions.When a php script finds a function call ; then immediately controls jumps into block of function and starts execution of block and control leaves it’s block only when it completes al statement’s execution.


PHP Functions | Example

Functions are “self contained” modules of code that accomplish a specific task.

Functions usually “take in” data, process it, and “return” a result.

Why do we use Functions in PHP

They allow us to reuse code instead of rewriting it.

Functions allow us to test small parts of our program in isolation from the rest

Functions can be reused in other php application using include keyword.

Example

";
	echo "
"; } function days_name($day_name){ echo "First day of Week is ".$day_name ; } function employee($name,$city){ echo $name." lives in ".$city; } function temp_today($temprature=20){ echo "Today's Temperature is " . $temprature; } fun();//Zero Argument days_name("Sunday");//One Argument echo "
"; echo "
"; employee("Harish","Delhi");//Two Argument echo "
"; echo "
"; temp_today(); ?>

Returning Values from Functions | Example

Example

";
 echo "5 +5=".addition(5,5)."
"; ?>

Passing Arguments to a Function by Reference

When we pass a value by address in function as a parameter; it’s value will get changed outside the function.If you do not want any change into your variable then pass it by value as parameter in your function’s argument.

Example

";
	return $number;	
}

$value=10;
echo $value;
echo "
"; echo addition($value); echo "
"; echo $value; ?>

PHP For Loop


PHP For Loop:– For Loop is also used for iteration which allows code to be executed repeatedly till the condition being true.


PHP For Loop | Example

Let us take an example to understand For loop.

Syntax

for(init value; test value; increment value){
//code for execution
}

Example

<?php
for($x=1;$x<5;$x++){
	echo $x;
	echo "<br>";
}
?>

PHP While Loop


PHP While Loop:– In php also all loops behave same as in other programming languages likw C,Java,C++ etc.When we want to iterate a condition till it is true then loops comes into picture.


PHP While Loop | Example

Let us take an exampmle to understand while loop in php.

Syntax

while(condition is true){
//code for execution
}

Example


";
	$x++;
}
?>


PHP Global Keyword


PHP Global Keyword:– Global keyword is used in php if we want to access a global variable inside a function.


PHP Global Keyword | Example

Let us take an exampmle to understand global keyword in php.

Example

";
	echo "hey";
	echo "
"; } play(); echo $x; echo $y;//compile error ?>


PHP Static Keyword


PHP static Keyword:– We know how a local variable works inside a function .No one allowed to access local keyword outside the function that simply means that a local variable dies after the execution of function completeness.
If one requires this local variable to access again and again so the only way is to put keyword static before variable name.


PHP static Keyword| Example

Let us take an exampmle to understand static keyword in php.

Example

";
game();
echo"
"; game(); ?>


PHP Variable Scope


PHP Variable Scope:– Scope of a variable means who can see and use it. In other words we can say that when we use a variable inside a function it refers to local variable and if it is defined outside the function ; it is called as globale variable because we can access it anywhere within the program.


PHP Variable Scope | Example

Let us take an example to understand scope of a variable.

Below we get two compile errors while executing below program because we are trying to access $x i.e global variable inside a function. Similarily we can not access a local variable outside the function.

Example

";
	echo "hey";
	echo "
"; } play(); echo $x; echo $y;//compile error ?>


PHP Switch


PHP Switch :– Switch control is helpful in comparing values against multiple conditions in order to execute matching statement as per the program requirement.

When switch expression evaluate a value then immediately control enters inside switch block and searches for a match value equivalent to expression value ; obvious the corresponding statement will be executed.

This scenerio can be easily understood by below mentioned php script.


PHP Switch | Case | Example

Let us take an example to understand how does switch case works. Here we can see that $name is variable name and it’s value is 20 which is passed as to switch parameter and the corresponding case condition is executed. So the output we got number is 20 shown in below screenshot.

PHP Switch Case Example



PHP Else if / Elseif

PHP Else if / Elseif :– It is very comman to write code using conditional statement when we take any decision on the basis of our business logic.


PHP Else if / Elseif | Example

Here we discuss all possible outputs in detail which may produce by following example for learning of conditional statements in php .

  1. Following is an example for how to write an If..Else..If in php. In elseif condition loop controller looks for true condition ,once it got it will stop the execution after this point.
  2. If no condition is true then only else statement will give output for the program by default.

Note:–

You can use both elseif or else if with space or without space in your editor to program a script.Both will absolutely work .But one thing here is to be remember that is if you write colon after elseif condition then only elseif without space will work.

Example


PHP Operators


PHP Operators:–Operators are oftenly used in programming languages statements. Hence operators are useful to perform operations on various variables.


PHP Operators | Example

There are several operators defined in php language which are listed below as follows:-

  1. Assignment operators
  2. Increment/Decrement operators
  3. Arithmetic operators
  4. Comparison operators
  5. Logical operators
  6. Array operators
  7. String operators

ArithMetic Operators Types | Example

Let us understand few of them with examples-There are six following arithmetic operators in php. Following table is an example which easily describes all arithmetic operators in brief.

 

Operator Symbol Name Example Meaning
Substraction $x-$y Difference of two numbers
* Multiplication $x*$y Multiplication of two numbers
/ Division $x*$y Division of two numbers
% Modulus $x%$y Modulus of two numbers
** Exponentiation $x**$y EResult after raising $x to the $y’th power
* Multiplication $x*$y Multiplication of two numbers

 

Comparison Operators Types | Example

These operators are used to compare values. Below table represents their usage in php. 

 

Operator Symbol Name Example Meaning
!= Not equal $a != $b Returns true if $x is not equal to $y
<> Not equal $a<>$b Returns true if $x is not equal to $y
!== Not identical a!==$b True if $x is not equal to $y, or they are not of the same type
=== Identical $a===$b Returns true if $x is equal to $y, and they are of the same type
== Equal $a==$b Returns true if $x is equal to $y
> Greater than $a>$b eturns true if $x is greater than $y
< Less than $a<<$b Returns true if $x is less than $y
<= Less than or equal to $a<=$b Returns true if $x is less than or equal to $y
>= Greater than or equal to $a>=$b Returns true if $x is greater than or equal to $y