Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, March 13, 2015

what are PHP Constants ?

A constant acts as an identifier (name) for a simple value. Valid constant name starts with a  letter or underscore, followed by any numbers or  letters or underscores. Constant name cannot start with $ sign.


Uses of PHP constant:

Constants are accessible from anywhere without regard to scope.
Constants acts like global and has a global scope, and are unchangeable they are defined once and can be called or used many times. Therefore, it is convenient using constants as it reduces the time for programming.
Syntax:

define(name, value, case-insensitive)

Example:

PHP Script (constant.php)
<?php
define("const1", "<h3>PHP Tutorial</h3>");
/*Case insensitive constant name*/
define("CONST2", "PHP - Hypertext Preprocessor", true);
echo "<h2>Example of Constant</h2>";
echo const1;
echo const2;
?>



PHP Array , print array php

Array in PHP is an ordered map, an ordered map that associates values to keys. It can hold ore then one value at a time. It can hold many values under a single name and also you can access these values by referring to an index number.

Syntax:
 $variable_name = array(value1,value2,value3); 

Array are categorised bades in dimension and keys



Array based on Dimensions:

Single Dimension:
Single dimension array have single index value. Singe dimension array are used to represent and store data in linear form called as single or one dimensional array.


Example:

PHP Script (singledimen.php)
 <?php  
 $website = array("blog", "www.nirajanghimireyblogspot.com","PHP");  
 echo "I like ".$website[0]." site having URL ".$website[1]." for ".$website[2]." course.";  
 ?>



Multi Dimension:
In multi dimension array each element in the main array can also be an array, hence it’s like an array consisting another array. Values in multi dimension array are accessed using multiple index.

Example: 

PHP Script (multidimensional.php)
 <?php  
 $website = array("blog", "www.nirajanghimireyblogspot.com",array("PHP","ASP","Other"));  
 echo "I like ".$website[0]." site having URL ".$website[1]." for ".$website[2][0]." course.";  
 ?> 

Array based on Keys :


Numeric array:

This  arrays can store numbers,  strings and any object but index are represented by numbers. In this array the default number is started from zero.

Example: 

PHP Script (numeric.php)
 <?php  
 $age = array('Joe' =>'23' ,'Andy'=>'27','Lakhan'=>'25' );  
 echo "Age of Joeis ".$age['Joe']." years old.";  
 ?>  


Associative array:

Numeric array and are very similar to numeric arrays, but they have their index as a string and association between  key and values are strong.

Example :


PHP Script (associative.php)
 <?php  
 $stdent=array(  
  "info"=>array("name"=>"Joe","dob"=>"2049"),  
  "marks"=>array("english"=>array("20","98"),"math"=>"50","science"=>"90"),"division"=>array("distic"=>"80","first"=>"60","2nd"=>"45"));  
 echo $stdent['division']['distic'];  
 ?> 

Monday, February 9, 2015

PHP Functions



In average PHP has more than 1000 built-in function; function are vital thing that gets PHP the real power. Beside 1000 + functions we also can create our own functions using logics. Functions are the block of statements that can be used multiple times in a program. they do not execute until they are called. A function name can be declared with a letter or underscore but not a number.

Uses of PHP functions

They are used to make a program run conveniently and error free.Function has different scope in PHP programming. All logic can be implemented using function, complex outputs are simply programmed with in a function etc.

Syntax:
 functionfunctionName() {  
   code to be executed;  
 }  


Example:

PHP Script (function.php)
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta charset="UTF-8"/>  
 <title>Nirajan Ghimirey's Workshop</title>  
 </head>  
 <body>  
 <?php  
 //Example of Function  
 function message(){  
   echo "Welcome to Nirajan Ghimirey's Workshopl<br/>";  
 }  
 //Example of function with arguments  
 function courses($name,$desc){  
   echo $name."=". $desc."<br/>";  
 }  
 //Example of default function  
 functiondefalultMessage($view=100){  
   echo "Viewer = ".$view."<br/>";  
 }  
 //Example of function with return value  
 function returnValue($value1,$value2){  
   $value3=$value1+$value2;  
   return $value3;  
 }  
 //function call  
 message();  
 courses("PHP","Hypertext Preprocessor");  
 defalultMessage();  
 echo "Users = ". returnValue(10,5);  
 ?>  
 </body>  
 </html> 


PHP Loops and their usage with example


PHP loops are used to execute a block of code for a specific number of times. It is very popular among coders and programmers as it reduces the effort and time to execute a script or scripts.

For loop:

For loop is used to run a single script for multiple times. It is used when you know how many times the script should run.
Syntax:
 for (initialization; condition; increment)  
 {  
  Here is code to be executed;  
 }  


Example:
For loop:
 <?php  
 /*Example of for loop*/  
 for ($i = 1; $i < 10; $i++) {  
   echo $i;  
   echo "<br/>";  
 }  
 ?> 

Do while loop:
This loop loops through a block of code once, and repets the loop as long as the condition is true.

Syntax:
do  
 {  
   Here is code to be executed;  
 }while (condition); 
Example:
PHP Script (do-while.php)
 <?php  
 /*Example of do-while loop*/  
 $i=0;  
 do {  
   $i++;  
   echo "$i";  
   echo "<br/>";  
 } while ($i < 10);  
 ?>

While loop:
This loop loops through a block of code as long as the given condition is true.


Syntax:
 while (condition) {  
   this is the code to be executed;  
 } 

Example:

PHP Script (while.php)
 <?php  
 $i = 1;  
 /*Example of while loop*/  
 while($i <= 15) {  
   echo "$i <br>";  
   $i++;  
 }  
 ?>  

Foreach loop:
It loops through a block of code for each element in an array.
Syntax:
 foreach ($array as $value) {  
   code to be executed;  
 }

Example:

PHP Script (foreach.php)
 <?php  
 /*Example of foreach*/  
 $arr = array("1","2","3","4","5","6","7","8","9","10","11","12");  
 foreach($arr as $value){  
   echo $value;  
   echo "<br/>";  
 }  
 ?>  

Syntax:
 foreach ($array as $value => $assoc_value) {  
   code to be executed;  
 }  

Example:

PHP Script (foreach-assoc.php)

<?php  
 /*Example of foreach for associative array*/  
 $age = array("HTML"=>"Hypertext Markup Language", "CSS"=>"Cascading Style Sheets", "PHP"=>"Hypertext Preprocessor");  
 foreach($age as $x => $y) {  
   echo "$x", " = " . $y;  
   echo "<br>";  
 }  
 ?>  

Wednesday, January 7, 2015

What are PHP Operators ?

Operations are performed in PHP using operators. These operators are used on variables and values.
There are seven operators in PHP:
·         Arithmetic Operators.
·         Assignment Operators.
·         Comparison Operators.
·         Increment/Decrement Operators.
·         Logical Operators.
·         String Operators.
·         Array Operators.


Details on operators:

1.       Arithmetic Operators: They are used to perform common arithmetical operations such as addition, subtractions etc. with numeric values.
Operator
Name
Example
Result
+
Addition
$x+$y
Sum of $x and $y
-
Subtraction
$x-$y
Difference of $x and $y
*
Multiplication
$x*$y
Product of $x and $y
/
Division
$x/$y
Quotient of $x and $y
%
Modulus
$x%$y
Remainder of $x divided by $y
**
Exponentiation
$x**$y
Result of raising $x to the $y’th power

Example:
PHP Script (arithmetic.php)
 <?php  
 //Example of arithmetic operator  
 $x = 20;  
 $y = 30;  
 echo $x + $y."<br/>";  
 echo $x-$y."<br/>";  
 echo $x*$y."<br/>";  
 echo $x/$y."<br/>";  
 echo $x%$y."<br/>";  
 echo $x**$y."<br/>";  
 ?>  


2.       Assignment Operators: They are used with numeric values to write a value to a variable.
Operator
Name
Description
x=y
x=y
The left operands gets set to the value of the expression on the right
x+=y
x=x+y
Addition
x-=y
x=x-y
Subtraction
x*=y
x=x*y
Multiplication
x/=y
x=x/y
Division
x%=y
x=x%y
Modulus

Example:
PHP Script (assignment.php)
 <?php  
 //Example of assignment operator  
 $x = 20;  
 $y = 30;  
 $x=$y;  
 echo $x."<br/>";  
 $x+=$y;  
 echo $x."<br/>";  
 echo $x-=$y;  
 echo"<br/>";  
 echo $x*=$y;  
 echo"<br/>";  
 echo $x/=$y;  
 echo"<br/>";  
 echo $x%=$y;  
 ?>  

3.       Comparison Operators: These operators are used to compare two values.
Operator
Name
Example
Result
==
Equal
$x==$y
Returns true if $x is equal to $y
===
Identical
$x===$y
Returns true if $x is equal to $y, and they are of the same type
!=
Not equal
$x!=$y
Returns true if $x is not equal to $y
<> 
Not Equal
$x<>$y
Returns true if $x is not equal to $y
!==
Not identical
$x!==$y
Returns true if $x is not equal to $y, or they are not of the same type
> 
Greater than
$x>$y
Returns true if $x is greater than $y
< 
Less than
$x<$y
Returns true if $x is smaller than $y
>=
Greater than or equals to
$x>=$y
Returns true if $x is greater than or equals to $y
<=
Less then or equals to
$x<=$y
Returns true if $x is less than or equals to $y

Example:

PHP Script (comparison.php)

<?php  
 //Example of comparison operator  
 $x = 20;  
 $y = 30;  
 var_dump($x == $y);echo "<br/>";//returns false because values are not equal  
 var_dump($x === $y);echo "<br/>";/*returns false because types are equal but value are not equal*/  
 var_dump($x != $y);echo "<br/>"; // returns true because values are not equal  
 var_dump($x <> $y);echo "<br/>"; // returns true because values are not equal  
 var_dump($x !== $y);echo "<br/>"; /*returns true because types are equal and values are not equal*/  
 var_dump($x > $y);echo "<br/>";// returns false because $x is not less than $y  
 var_dump($x < $y);echo "<br/>";// returns true because $x is less than $y  
 var_dump($x >= $y);echo "<br/>";// returns false because $x is less than $y  
 var_dump($x <= $y); // returns true because $x is less than $y  
 ?>  

4.       Increment/ Decrement Operators: They are used to either for increment or decrement a variable value.
Operator
Name
Description
++$X
Pre-increment
Increments $x by one, then returns $x
$X++
Pose-increment
Returns $x, then increments $x by one
--$X
Pre-decrement
Decrements $x by one, then returns $x
$X--
Pose-decrement
Returns $x, then decrements $x by one

Example:
PHP Script (incre-decre.php)
 <?php  
 $x = 20;  
 echo ++$x."<br/>";  
 echo $x++."<br/>";  
 echo --$x."<br/>";  
 echo $x--;  
 ?>  

5.       Logical Operators:Logical operators are used to combine conditional statements.
Operator
Name
Example
Result
and
And
$x and $y
True if both $x and $y are true
or
Or
$x or $y
True if either $x or $y is true
xor
Xor
$x xor $y
True if either $x or $y is true, but not both
&&
And
$x&&$y
True if both $x and $y are true
||
Or
$x || $y
True if either $x or $y is true
!
Not
!$x
True if $x is not true

Example:

PHP Script (logical.php)

 <?php  
 $x = 20;  
 $y = 30;  
 if ($y == $x and $y==30) {  
   echo "This Tutorial";  
 }  
 echo "<br/>";  
 //example of or operator  
 if ($y == $x or $y==30) {  
   echo "Workshop Tutorial";  
 }  
 echo "<br/>";  
 //example of xor operator  
 if ($x==20 xor $x==$y ) {  
   echo "Learn HTML";  
 }  
 echo "<br/>";  
 //example of && operator  
 if ($x==20 && $y==30 ) {  
   echo "Learn CSS";  
 }  
 echo "<br/>";  
 //example of || operator  
 if ($x==20 || $x==$y ) {  
   echo "Learn JavaScript";  
 }  
 echo "<br/>";  
 //example of ! operator  
 if ($x!=$y) {  
   echo "Learn PHP";  
 }  
 ?> 

6.       String Operator: They are specially designed for strings.
Operator
Name
Example
Result
.
Concatenation
$ft1.$ft2
Concatenation of $txt1 and $txt2
.=
Concatenation assignment
$ft1 .= $ft2
Appends $txt2 to $txt1


Example:

PHP Script (logical.php)

 <?php  
 $x = "Nirajan";  
 $y = "Workshop";  
 //Example of . operator  
 echo $x.$y."<br/>";  
 //Example of .= operator  
 echo $x.=$y;  
 ?>  


7.       Array Operator: They are used to compare array in PHP.
Operators
Name
Example
Result
+
Union
$x + $y
Union of $x and $y
==
Equality
$x == $y
Returns true if $x and $y have the same key/value pairs
===
Identity
$x === $y
Returns true if $x and $y have the same key/value pairs in the same order and of the same types

!=
Inequality
$x != $y
Returns true if $x is not equal to $y
<> 
Inequality
$x <> $y
Returns true if $x is not equal to $y
!==
Non-identity
$x !== $y


Example:

PHP Script (array.php)

 <?php  
 $x = array("H" => "HTML", "C" => "CSS");  
 $y = array("P" => "PHP");  
 //Example of array operator +  
 print_r($x + $y);echo"<br/>";  
 //Example of array operator ==  
 var_dump($x == $y);echo"<br/>";  
 //Example of array operator ===  
 var_dump($x === $y);echo"<br/>";  
 //Example of array operator !=  
 var_dump($x != $y);echo"<br/>";  
 //Example of array operator <>  
 var_dump($x <> $y);echo"<br/>";  
 //Example of array operator !==  
 var_dump($x !== $y);  
 ?>