Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Monday, June 3, 2013

What is PHP, Introduction of PHP, PHP Programming, Web Development with PHP


Core PHP :-

PHP is a server side scripting language which is used to develop the Dynamic web pages to interact with the database applications and can be develop to enterprise applications. The PHP scripts is very simple and can be embed easily with the HTML.



Monday, May 27, 2013

What is PHP?, Full Form of PHP


PHP Introduction

PHP is a server side scripting language which is used to develop the Dynamic web pages to interact with the database applications and can be develop to enterprise applications. The PHP scripts is very simple and can be embed easily with the HTML.


  • The Hypertext Preprocessor (PHP) is a Server side Scripting Language for design and developing Dynamic and Interactive Web Pages.The PHP support the Databaseconnectivity with many latest Databases like MySql, Sql Server, Oracle etc.

  • PHP is widely used General purpose scripting language that is especially suited for web development and can be easily embedded into HTML code. The PHP  support HTML, CSS and JavaScript also.
  • PHP is open source and freely available.
  • PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP, JSP etc.

Wednesday, May 1, 2013

in_array function in PHP, How to find an element in an array in PHP, Difference between in_array() and array_search() function in PHP


in_array function in PHP, How to find an element in an array in PHP, Difference between in_array() and array_search() function in PHP

If you want to determine whether a value exists within a provided array, use the in_array() function. This function checks data within the array in question and returns true or false accordingly. If you want to get the value of the searched element rather then the true or false value use the array_search() function.

Difference between in_array() and array_search() function in PHP

in_array() used to check if the element is exist or not into the given array and the array_search() used to print the element which we want to find.


Example


<?php
$marks = array(87,88,98,74,56,94,67,98,49) ;
echo "<b>The all elements of the Array are</b><br/>";
foreach($marks as $mark)
echo "$mark<br/>" ;
if (in_array(49, $marks) ) {
echo "The 49 Element is found at the position : " . array_search(49, $marks) ;
} else {
echo "49 Not found in given array" ;
}
?>

Output

The all elements of the Array are
87
88
98
74
56
94
67
98
49
The 49 Element is found at the position 8

Tuesday, April 30, 2013

array_unique() function in PHP, How to display unique elements of an Array in PHP, How to remove duplicate elements from an array PHP


array_unique() function in PHP, How to display unique elements of an Array in PHP, How to remove duplicate elements from an array PHP

If you want to make sure all the elements in a provided array are unique, use the array_unique() function. This function removes duplicity from the Array and shrinks the array if there are in fact duplicate values. The element keys are not renumbered when this function is complete.

example

<?php
$grades = array(87,88,98,74,56,94,67,98,49) ;
echo "<b>The all elements of the Array are</b><br/>";
foreach($grades as $grade)
echo "$grade<br/>" ;
$uniqueGrades = array_unique($grades);
echo "<b>The unique elements in the Array are</b><br/>";
foreach($uniqueGrades as $ugrade)
echo "$ugrade<br/>" ;
?>

Output

The all elements of the Array are
87
88
98
74
56
94
67
98
49
The unique elements in the Array are
87
88
98
74
56
94
67
49


PHP Tutorials - Math Functions in PHP, Math functions on Array in PHP


Math Functions in PHP, Math functions on Array in PHP


The PHP provides the several math-type functions which are used to perform the math operation on arrays. The array_sum() function is used to adds the vale of all stored elements of an array and returns that answer to a variable. The count() function is used to count the number of elements of an array returns the number of elements in a variable.
Here is a simple example to demonstrate these two functions
<?php

$marks = array(87,88,98,74,56,94,67) ;
$sum_of_marks = array_sum($marks);
echo "The sum of the provided Marks is: $sum_of_grades <br/>";
$avgMarks = array_sum($marks) / count($marks) ;
echo "and the average of these Marks is: " . round($avgMarks,2) ;
?>

Output
The sum of the provided Marks is: 564
The average of these Marks is: 80.57

I have used the round() function to round the value of average grades into two decimal places.

PHP Tutorials - How to Sort Arrays in PHP, Different Sorting Methods in PHP


How to Sort Arrays in PHP, Different Sorting Methods in PHP


The Sorting is a technique to ordering an array's element by ascending or descending order.The PHP provides the number of sort functions to do this. The sort() function sorts the array based on the values in it in ascending order, The rsort() function does exactly the same thing but it sorts the array in descending order. The ksort() and krsort() sorts array by the associated key. 
The following examples shows the each sort function with example.


Regular Sorting

<?php
$Colors = array("Red", "Pink","Greeen",
"Blue","Yellow" ) ;
echo "<strong>Before sorting the array is</strong><br/>";
foreach ($Colors as $color) {
echo $color . "<br/>";
}
sort ($Colors) ;
echo "<strong>After Sorting the array is</strong> <br/>";
foreach ($Colors as $color) {
echo $color . "<br/>";
}

?>

output
Before sorting the array is
Red
Pink
Greeen
Blue
Yellow
After Sorting the array is 
Blue
Greeen
Pink
Red
Yellow 


Reverse Sorting

$Colors = array("Red", "Pink","Greeen",
"Blue","Yellow" ) ;
echo "<string>Before sorting the array is</strong><br/>";

foreach ($Colors as $color) {
echo $color . "<br/>";
}

rsort ($Colors) ;
echo "<strong>After Sorting the array is</strong> <br/>";
foreach ($Colors as $color) {
echo $color . "<br/>";
}

output
Before sorting the array is
Red
Pink
Greeen
Blue
Yellow
After Sorting the array is 
Yellow
Red
Pink
Greeen
Blue





Sorting with Keys in ascending order


$Colors = array("open"=>"PHP", "Microsoft"=>"ASP","Sun"=>"Java",
"IBM"=>"DB2","oracle"=>"oracle" ) ;
echo "<string>Before sorting the array is</strong><br/>";

foreach ($Colors as $color) {
echo $color . "<br/>";
}

rsort ($Colors) ;
echo "<strong>After Sorting the array is</strong> <br/>";
foreach ($Colors as $color) {
echo $color . "<br/>";
}
output
Before sorting the array is
PHP
ASP
Java
DB2
oracle
After Sorting the array is 
DB2
ASP
Java
PHP
oracle


Sorting with Keys in descending order

$Colors = array("open"=>"PHP", "Microsoft"=>"ASP","Sun"=>"Java",
"IBM"=>"DB2","oracle"=>"oracle" ) ;
echo "<string>Before sorting the array is</strong><br/>";

foreach ($Colors as $color) {
echo $color . "<br/>";
}

krsort ($Colors) ;
echo "<strong>After Sorting the array is</strong> <br/>";
foreach ($Colors as $color) {
echo $color . "<br/>";
}
output
Before sorting the array is
PHP
ASP
Java
DB2
oracle
After Sorting the array is 
oracle
PHP
Java
ASP
DB2




Saturday, March 30, 2013

How to Send Email From Localhost to Gmail with PHP - Sending Email from Wamp Server to any other email account


Sending Email from Localhost Wamp Server to Gmail account with PHP script.




The PHP mail() function is not sufficient to send email from localhost and we have to purchase an email server to send email from php mail() function and we need to change some parameter in php.ini file, but if you want to check your email form on localhost and do not want to purchase any email server then here is a good tutorial for sending email with php code without changing any SMTP port and parameter values.

You just only be provide your gmail account and password in it and you can easily send email to any other account.


Here is a complete code for sending Email Download Code





Saturday, February 23, 2013

Arrays in PHP - How to create an Array in PHP - Types of array in PHP


Arrays in PHP - How to create an Array in PHP - Types of array in PHP
Arrays are the special variables in PHP which can store more then one value into a single variable. In PHP a array() function is used to create an array.The PHP array can store any kind of value into it and combination of different type of values into the array variable.

Example for creating an array in PHP:-

<?php

$colors=array("red","green","blue","white","black");
?>

Example for printing an array in PHP:-


<?php

$colors=array("red","green","blue","white","black");
echo $colors[0].",".$colors[1].",".$colors[2].",".$colors[3].",".$colors[4];

?>

There are three types of array in PHP:-


  • indexed array.
  • associative array.
  • multidimensional array. 
Indexed array :- arrays with numeric index or you can say these type of arrays are similar to the c, java's array.

>>two way to creating the indexed arrays in PHP

<?php
$colors=array("red","green","blue");
?>

<?php
$colors[0]="red";
$colors[1]="green";
$colors[2]="blue";
?>

Associative array :- arrays with a key which is associative with each  value.

>>two way to creating the associative arrays in PHP

<?php
$colors=array("1"=>"red","2"=>"green","3"=>"blue");
?>

<?php
$colors['1']="red";
$colors['2']="green";
$colors['3']="blue";
?>

printing an associative array:

<?php
$colors=array("1"=>"red","2"=>"green","3"=>"blue");
echo $colors['1'].",".$colors['2'].",".$colors['3'];
?>

Multidimensional array :- An array which can contain another array as a value is called multidimensional array. we can create two or three dimensional array as well.

Constructing multi-dimensional array:-

<?php
$colors=array
(
array("1"=>"red","2"=>"white");
array("3"=>"green","4"=>"blue");
);
?>






Switch Statement in PHP - How to use Swich Case in PHP


Switch Statement in PHP - How to use Swich Case in PHP

Switch Statement is used to choose one of the statement from many of choices. It is used to give a menu driven output to the user.

Syntax to use switch case statement in php.

<?php

switch(choice)
{
case label1:
statement;

case label2:
statement;
................
...............
...............

case labelN:
statement;
default:
code which should be execute if any of choice is not matched.



}
?>
Example:

<?php
$code=1;
switch($code)
{
case 1:
echo "Red";
break;
case 2:
echo "Green";
break;
case 3:
echo "Blue";
break;
default:
echo "wrong code";
}

?>


Tuesday, February 19, 2013

Operators in PHP - List of Operators in PHP, How to use Operators in PHP


Operators in PHP - List of Operators in PHP, How to use Operators in PHP

Symbols which are used to perform some types of operation on operands are called operators the php support all king of operators which are used in other languages. The following type of operators are php support-


  • Arithmetic Operators
  • Assignment Operators
  • Increment/Decrements Operators
  • Comparison Operators
  • Logical Operators
  • Array Operators   

Arithmetic Operators :-

(+) Operator - Used to add two integers or real numbers.
(-) Operator - Used to subtract two integers or real numbers.
(*) Operator - Used to multiply two integers or real numbers.
(/) Operator - Used to add divide integers or real numbers.
(%) Operator - Used to find Remainder of two integers or real numbers.

Assignment Operators:-

(=) Operator - used to assign a value to a variable.
(+=) Operator - used to add two variables and store it to a variable.
(-=) Operator - used to subtract two variables and store it to a variable.
(*=) Operator - used to multiply two variables and store it to a variable.
(/=) Operator - used to divide two variables and store it to a variable.
(%=) Operator - used to find remainder of two variables and store it to a variable.


Increment/Decrements Operators:-

(x++) - used for Post Increment.
(++x) - used for Pre Increment.
(x--) - used for Post Increment.
(--x) - used for Pre Increment.





String Variables in PHP - PHP Strings with Example - How to Concatenate Strings in PHP


String Variables in PHP - PHP Strings with Example - How to Concatenate Strings in PHP 

As you know the PHP is loosely typed programming language and there is no data types so the simple variables are used to store the string in PHP and there are number of methods/functions for manipulate the string variables further.

For Example:-

<?php
$str="Hello PHP String";
echo $str;
?>

String Concatenation in PHP:-

As like other languages like Java, C# the php does not use the (+) symbol for concatenation of two or more strings here the (.) symbol used for this purpose.
Example:-

<?php
$str1="Hello";
$str2="PHP";
echo $str1.$str2;
?>



Variables Scope in PHP - Availability of Variables in PHP program - PHP Variables Scope


Variables Scope in PHP - Availability of Variables in PHP program - PHP Variables Scope 

There are 3 types of Variables Scope available in PHP. The Scope concern the variables availability for their use in our program-


  • local
  • global
  • static
Local Scope :- To know about local scope of a variable you need to see an example so i am showing a good example to you.

<?php
$x;  //global scope

function show()
{
$str="Hello PHP"; //local scope can't be access outside the function
echo $str;
}

?>

Global Scope:- To know about global scope of a variable you need to see an example so i am showing a good example to you.

<?php
$x="Hello Global";  //global scope

function show()
{
$str="Hello PHP"; //local scope can't be access outside the function
echo $str;
echo global $x;    //global keyword is used to access global variables
}

?>

Static Scope:- When a function work is completed normally its variables values are deleted but if you want the function's variables should not delete and remain its previous values then we use the "static" keyword before declaring it.
Example:-

<?php
$x="Hello Global";  //global scope

function show()
{
$str="Hello PHP"; //local scope can't be access outside the function
static $x=0;
$x++;
}
show();
show();
show();
?>

According to the above example the show() function's variable x's last value is 3 because it is static variable.

How to Declare Variables in PHP - PHP Variables Creation


How to Declare Variables in PHP - PHP Variables Creation

As like all other languages the purpose of variables in PHP is same. As like C, C++, C#, Java and VB the variables are used to store the values.

But as like C, C++, C#, Java and VB the PHP variables are not strictly typed, In PHP the variables are loosely typed and and no need to specify the Data Types of any variable at the time of declaration. The PHP variables implicitly convert to its assigning values.

The PHP variables should start with $ sign whenever they used.

For Example:-

<?php
$str;
$x=10;
$str="Hello PHP Variables";
$y=10.5;
echo $str;
echo $x;
echo $y;
?>




Simple Program in PHP - PHP Syntex of a Program, How to write a simple Program in PHP


Simple Program in PHP - PHP Syntex of a Program, How to write a simple Program in PHP

There are two ways to work with PHP one is to write PHP scripts into <?php ------some code-----?> and second is <? -----some code----?> .
I am suggest you to use the first one because the second one is not supported all scripts so lets come to how to write a simple program in PHP.

<html>
<head>
<title>Simple Program in PHP</title>
</head>
<body>
<?php
echo "Welcome to the World of PHP";
?>
</body>
</html>

the PHP script can be placed any where into our program like i am using it into the body section. It depends on your requirement.


Monday, February 18, 2013

How to Install PHP - PHP Installation - Download WAMP Server for PHP


How to Install PHP - PHP Installation - Download WAMP Server for PHP

To work with PHP you need to install a Web Server, PHP and MySql.


Download Apache Tomcat Web Server from apache website  
Download PHP from PHP website
Download MySql from MYSql Site

If you don't want to download these 3 software's then another Alternative is download the WAMP Server, The WAMP server has all three software's in one package.

Download WAMP Server from Wamp website
you need to download a small software

Microsoft Visual C++ 2010 SP1 Redistributable Package from Microsoft website


first install the 

Microsoft Visual C++ 2010 SP1 Redistributable Package and then install the WAMP Server

Now you are ready to work with PHP
Write your Program with extension .php and place it to your web directory 
Friday, February 8, 2013

What is PHP - Full Form of PHP - Hypertext Preprocessor



What is PHP - Full Form of PHP - Hypertext Preprocessor
  • The Hypertext Preprocessor (PHP) is a Server side Scripting Language for design and developing Dynamic and Interactive Web Pages.The PHP support the Database connectivity with many latest Databases like MySql, Sql Server, Oracle etc.
  • PHP is widely used General purpose scripting language that is especially suited for web development and can be easily embedded into HTML code. The PHP  support HTML, CSS and JavaScript also.
  • PHP is open source and freely available.
  • PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP, JSP etc.


The PHP script only written into <?php  ?> or <?   ?> tags the first one is given more facilities for writting PHP scripts.

The sample program for just printing a Hello PHP message.

<?php

echo "Hello PHP";

?>

save it with .php extension 

Tuesday, January 1, 2013

Conditional Statements in PHP - PHP if else if with Example


Conditional Statements:-

In every program you need to perform different actions for different tasks so you need to use the conditional statements to do these type of actions.

In PHP there are several types of conditional statements.

if statements:- if statement is used to execute some code inside the if block, if the specified condition is true then the code will be execute otherwise the code never execute.

Example:-

<?php
$x=0;
if($x>0)
{
echo $x;
}

?>

if else Statements:- if else statement is used to execute some code inside the if block, if the specified condition of if block is true then the code will be execute otherwise the else code should be execute.


Example:-

<?php
$x=0;
if($x>0)
{
echo "Hi";
}
else
{
echo "Bye";
}
?>

if else if Statements:- if else if statement is used to execute some code inside the multiple choice if blocks, the compiler checks all conditions till its not find the true statement, if a true statement is found the compiler does't check next if statements and if all condition are false then the else condition should be execute.


Example:-

<?php
$x=0;
if($x>0)
{
echo "Hi";
}
else if($x==0)
{
echo "Bye";
}
else
{
echo "Wrong choice";
}
?>