PHP Basics Part -1

PropellerAds

PHP: Hypertext Preprocessor

is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML

How to Use PHP in WebPage?

PHP source code is embedded in an HTML-based document, and is identified by special delimiting tags,

<?php   content   ?>

How to Create PHP file?

Using .php filename extension on source file.

Variables in PHP

Variables is nothing but store the user information which can used by program,Variable are declared with leading $sign and the name of variable and Assignment Operator is used “=”

Lets Declare PHP variable

For Example

<?php

$my_name="Joehn Doe";

?>

 

PHP variables Type

  • Integers -integer number like 10 ,12 etc
  • Strings -character string like “India”
  • Doubles like 10.1 ,11.3
  • Booleans like  true or false
  • NULL -contain NULL value
  • Arrays -list contain index and value
  • Objects -Objects are the things you create out of a class ,you can create objects based on how you designed your class. The process of creating an object is called instantiation. You can create an object by creating a variable which stores the object, and using the new keyword and the name of the class to instantiate from
  • Resources

Rules for declaring php varibles

  1. All variables in PHP start with a $ (dollar) sign followed by the name of the variable.
  2. A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.
  3. If a variable name is more than one word, it can be separated with an underscore (for example $employee_code instead of $employeecode).
  4. ‘$’ is a special variable that can not be assigned.

What is Object?
The process of creating an object is called instantiation. You can create an object by creating a variable which stores the object, and using the new keyword and the name of the class to instantiate from
Lets take an Example how to create object and its use

What is Function ?
Function is set of codes which can perform specific tasks,the advantage of function is that it can be reusable once defined
For example

What is Class ?
Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.

Lets understand now what is Class in php and how to define a class? A class is what you use to definethe properties, methods and behavior of objects Below is the example and Syntax regarding class defining

<?php

class Address{

var $first_name;

var $street_address;

var $state;

var $district;

function getAddress(){

return $street_address;

}

}

?>

 

How to instantiate class ?

Syntax:

$objectName = new nameOfClass();
$objAddress= new Address();
How to Call Function ?

Syntax:

$object->functionName();
$objAddress->getAddress();

Constants in PHP
Constants are defined using define() function, which accepts two arguments: the name of the constant, and its value.

echo Statement
echo is a language construct not actually a function
we can use it without parentheses e.g. echo or echo()
Syntax

print Statement
Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print().

Operators in PHP
+ Addition $a + $b Sum of $a and $b
– Subtraction $a – $b Difference of $a and $b.
* Multiplication $a * $b Product of $a and $b.
/ Division $a / $b Quotient of $a and $b
% Modulus $a % $b Remainder of $a divided by $b

= Assign $a = $b $a = $b
+= Add and assign $a += $b $a = $a + $b
-= Subtract and assign $a -= $b $a = $a – $b
*= Multiply and assign $a *= $b $a = $a * $b
/= Divide and assign quotient $a /= $b $a = $a / $b
%= Divide and assign modulus $a %= $b $a = $a % $b
== Equal $a == $b True if $a is equal to $b
=== Identical $a === $b True if $a is equal to $b, and they are of the same type
!= Not equal $a != $b True if $a is not equal to $b
<> Not equal $a <> $b True if $a is not equal to $b
!== Not identical $a !== $b True if $a is not equal to $b, or they are not of the same type
< Less than $a < $b True if $a is less than $b > Greater than $a > $b True if $a is greater than $b
>= Greater than or equal to $a >= $b True if $a is greater than or equal to $b
<= Less than or equal to $a <= $b True if $a is less than or equal to $b
++$a Pre-increment Increments $a by one, then returns $a
$a++ Post-increment Returns $a, then increments $a by one
–$a Pre-decrement Decrements $a by one, then returns $a
$a– Post-decrement Returns $a, then decrements $a by one
and And $a and $b True if both $a and $b are true
or Or $a or $b True if either $a or $b is true
xor Xor $a xor $b True if either $a or $b is true, but not both
&& And $a && $b True if both $a and $b are true
|| Or $a || $b True if either $$a or $b is true
! Not !$a True if $a is not true
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1
+ Union $a + $b Union of $a and $b
== Equality $a == $b True if $a and $b have the same key/value pairs
=== Identity $a === $b True if $a and $b have the same key/value pairs in the same order and of the same types
!= Inequality $a != $b True if $a is not equal to $b
<> Inequality $a <> $b True if $a is not equal to $b
!== Non-identity $a !== $b True if $a is not identical to $b

If…Statements

if(condition){
// Code to be executed
}

 

if…else Statement

if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}

 

if…elseif…else Statement

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
}

 

Switch…Case

switch(day){
case sat:
// Code to be executed if day=sat
break;
case sunday:
// Code to be executed if day=sunday
break;
...
default:
// Code to be executed if day is different from all labels
}

 

Loops in PHP
while Loop

while(condition){
// Code to be executed
}

 

do…while Loop

do{
// Code to be executed
}
while(condition);

 

for Loop

for(initialization; condition; increment){
// Code to be executed
}

 

foreach Loop

foreach($array as $value){
// Code to be executed
}

 

PHP GET and POST
GET and POST methods are used to send information to server

GET Method
When the user enters information in a form and clicks Submit , there are two ways the information can be sent from the browser to the server: in the URL, or within the body of the HTTP request.
The GET method, which was used in the example earlier, appends name/value pairs to the URL. Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters. The URL could be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data. Also, parameters passed on the URL are visible in the address field of the browser not the best place for a password to be displayed

POST METHOD
Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

Below is the example how to use get and post method

if (isset($_GET['s'])) {
$temp = $_GET['s'];
}
else {
$temp = $_POST['s'];
}

 

$_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE