Sunday, 9 April 2017

php function with example

php functions with example
A user defined function declaration starts with the word "function":

Syntax
function functionName() {
code to be executed;
}
Note: A function name can start with a letter or underscore (not a number).

Tip: Give the function a name that reflects what the function does!

Note Function names are NOT case-sensitive.
In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name:

<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg(); // call the function
?>

Information can be passed to functions through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}

familyName("Janis");
familyName("Hedge");
familyName("Stadle");
familyName("Kait Jim");
familyName("Borgde");
?>

The following example has a function with two arguments ($fname and $year):

Example
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>

php function with argumemts

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

php function with return

<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>

For loop with example

for loop with example

The for loop is used when you know in advance how many times the script should run.

Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters:

init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 10:

<?php 
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";

?>

Syntax
foreach ($array as $value) {
code to be executed;
}

<?php 
$colors = array("w3schoolworld", "green", "blue", "yellow"); 

foreach ($colors as $value) {
echo "$value <br>";
}
?>

php loops with example

php loops with example

In PHP, we have the following looping statements:

while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array

Syntax
while (condition is true) {
code to be executed;
}

<?php 
$x = 1; 

while($x <= 5) {
echo "The number is: $x <br>";
$x++;

?>

Syntax
do {
code to be executed;
} while (condition is true);

<?php 
$x = 1; 

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.

The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked:

Example
<?php 
$x = 6;

do {
echo "The number is: $x <br>";
$x++;
} while ($x<=5);
?>

php if else statement with example

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

if statement - executes some code only if a specified condition is true
if...else statement - executes some code if a condition is true and another code if the condition is false
if...elseif....else statement - specifies a new condition to test, if the first condition is false
switch statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if statement is used to execute some code only if a specified condition is true.

Syntax
if (condition) {
code to be executed if condition is true;
}
The example below will output "Have a good day!" if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
echo "Have a good day!";
}
?>

Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

<?php
$t = date("H");

if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

Syntax
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

<?php
$t = date("H");

if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

php switch statement

php switch statement

Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

<?php
$fcolor = "red";

switch ($fcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

PHP operators 

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators

Wednesday, 2 November 2016

PHP 5 Constants

PHP 5 Constants
Constants are like variables except that once they are defined they cannot be changed or undefined.
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).
Note: Unlike variables, constants are automatically global across the entire script.
Create a PHP Constant
To create a constant, use the define() function.
Syntax
define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
The example below creates a constant with a case-sensitive name:
Example
<?php
define("GREETING", "Welcome to W3Schoolworld.com!");
echo GREETING;
?>
The example below creates a constant with a case-insensitive name:
Example
<?php
define("GREETING", "Welcome to W3Schoolworld.com!", true);
echo greeting;
?>
Constants are Global
Constants are automatically global and can be used across the entire script.
The example below uses a constant inside a function, even if it is defined outside the function:
Example
<?php
define("GREETING", "Welcome to W3Schoolworld.com!");
function myTest() {
    echo GREETING;
}

myTest();
?>

remove a property from a JavaScript object?

remove a property from a JavaScript object? delete myObject.regex; // or, delete myObject['regex']; // or, var prop = ...