You are on page 1of 7

PHP :-

1) PHP stands for Hypertext Preprocessor.


2) PHP is mainly used to create dynamic web application, web contents, web
elements.
3) It also provides the functionality to perform some IO tasks like to create
files, append or write files, delete files etc.
4) It is also capable to communicate with DB.
5) It is widely used server side scripting language.
6) It is free and open to use, so, all the popular servers like apache, iis etc
provides the default supports of PHP.
7) It can consume data from html form, and also provides the functionality to send
and receive data from COOKIE and SESSION

<?php ?> tags is used to write php codes, The tag should starts with <?php and ends
with ?>

Example :-

index.php

<h1>Welcome to PHP Session :-</h1>


<?php
$greet = "Hello world";
print($greet);
echo $greet;
print $greet;
echo "Greet :- $greet";
echo "Greet :- ".$greet;
echo "Greet :- <h1>".$greet."</h1>";
?>

Decision Making :-

1) if(condition){
//codes
}

2) if(codition){
//codes
} else{
//codes
}

3) if(condition){
//codes
}else if(condition){
//codes
}else{
//codes
}

4) if(condition){
//codes
if(condition){
//codes
}else{
//codes
}
}else{
//codes
if(condition){
//codes
}else{
//codes
}
}

Loops :- If we want to execute particular block of codes for several times based on
some condition then we can go for loops

while :-
$i=0
while($i<=10){
//codes
$i++;
}

do_while:-
$i=0;
do{
//codes
$i++;
}while($i<=10);

for:-
for($i=0;$i<=10;$i++){
//codes
}

Operators :-

1) Arithematic OPTRs :- +, -, /, *, %

2) Logical OPTRs :- &&(and) , ||(or)

3) Relational / Comparison OPTRs :- ==,!=, <, >, <=, >=

4) Assignment OPTRs :- =, +=, -=, /=, *=, %=

5) Increement / Decrement OPTRs :- ++, --

6) Ternary OPTRs : ?, :

$i=$i+1;
$a=10,$b=20;
$c=$a+$b;
$a+=$b; //$a=$a+$b
if($num%2==0){
echo "Even";
}else{
echo "Odd";
}

echo ($num%2==0)?"Even":"Odd";
Functions :-

The functions are nothing but the block of codes, which can be run on demand. Which
means, if we want to execute particular set of codes for several times and on
different places then it is better to create the function and re-use it to other
places.

Example :-

function myFunction(){
//code
}

The function keyword with function name is use to create function. It may or may
not have parameters and return statement. If we want to use functional output
outside the function, then we need to use return statement.

Parameterized function :-
function myFunction($param){
//code
}

Function with return statement :-


function myFunction($param){
return $param;
}

String :- PHP provides a rich string library to manipulate bulk string data. The
string is nothing but the CHAR SEQUENCE or the collection of multiple characters.
Whether it can be normal values, a textual file data, a textual web content etc. We
can use string library to manipulate all those data.

Some important functions are :-

1) explode() :- If we want to break the string base on a prticular delimiter.


2) implode() :- return array values as a single string.
3) join() :- same as implode, we can use it as alternative.
4) lcfirst() :- use to lower the case of first character.
5) ltrim() :- use to remove extra spaces from left of the string.
6) rtrim() :- use to remove extra spaces from right of the string.
7) str_split() :- Just like explode, it will break the single string and return the
array.
8) str_word_count() :- us to get the count of words contains in a particular
string.
9) strcasecmp() :- use to compare 2 strings without case.
10) strcmp() :- use to compare 2 string with case.
11) strpos() :- get the index of a particular string from another string.
12) strlen() :- get the string length.
13) strtolower() :- convert all string to lowercase.
14) strtoupper() :- convert all string to uppercase.
15) trim() :- remove extra spaces from left and right of the string
16) ucfirst() :- change the case of first character to upper case.

Arrays :- Use to store multiple values in single variable. It can be hetrogeneous,


growable and shrinkable in nature. The indexed based array is used with index
number starts with 0. The associative array is used to store values in key-value
pairs. The multi-dimensional array is nothing but the array of an array.
Syntax :-

$car = array("","","");

Note :- to print array in php, we print_r() function is use.

DateTime :- The PHP is also provide date library to work current date and time.
Syntax :-
$d=date();
print($d);//print current date and time.

////////////////////File IO////////////////////

The PHP also provides the feature to perform some IO tasks, like to read files,
write files, append files, move files, etc.

PHP contains some predefined functions manipulate files and folders, some important
functions are as follows :-

1) fopen(); need to pass file location as a first parameter and mode as a second
param.
2) fclose(); while perform some io task need to close the file after the end of
program. The file location or file object need to pass as parameter
3) fread(); to read the file in PHP, we need to open the file in read mode, and
also need to pass the filesize as a second parameter to indicate that, how much
data we need to read.
4) fwrite(); writing data into file is so easy in php, we just need to pass some
textual contents directly to write function and file object as well;
5) unlink(); use to delete the file. and the object of file is required.
6) filesize(); will return the size file.
7) fgets(); read file line by line.
8) fgetc(); read file character by character.
9) feof(); use to check end-of-file.

////////////Form Handling///////////////

In PHP to get the data from user we need to create the form and submit it to
particular php, the php script will handle or collect the form data.

Here are some important things we need to keep preserved :-

1) The html form should contains action attribute, which will have the name for php
file where we need to submit data.
2) We need to mention the method of form submission which can be GET or POST.
3) The form should contains some fields for user input and a submit button, which
will help to redirect the form to another php file.
4) To collect the data from html form, php provides superglobal variables $_GET and
$_POST which is accessible in all php scripts.
5) If we are submitting form data with GET method so we use $_GET to collect form
data else $_POST is used with POST method.
6) If we send data with GET method, data will get expose in url, and we can pass
maximum 256kb of textual data with GET method.
7) POST method does not have any limit for sending data. And it does not expose
sensitive in url. Which is secure than GET.

Note :- PHP provides isset() function to check the values is set in the variable.
It will help for check the required field contains value or not. We can use to
achive basic form validation for required fields.
//////////////COOKIE///////////////

In PHP, we can use some piece of data over the client side with the help of
setcookie() function provided by the PHP. Cookie always stored at client side, so
we can check in our browser, what site stores what kind of cookies in our browser.

setcookie() will accept name, value, expiry and the path to store cookie. PHP
provides the global variable $_COOKIE, which is used to get cookies from the
browser. We can store textual data in cookie with some memory limitations. It is
not recommended to store sensitive data in cookie. $_COOKIE is an associative
array, so we need to pass the key inside [] of $_COOKIE like $COOKIE["user"]

///////////Exception//////////////

Exception :- it is an object of Exception class which will terminate the php script
unexpectedly. To continue with the flow of script we need to handle those
exceptions with try_catch_finally blocks. If the try block contains some exception,
so the cursor will move in catch block. And if we want to perform some important
task, so we need to use finally block. Because if there is an exception then cursor
will move to catch else not. But finally block will execute compulsorily. If we
want to throw an exception with customize message, we can use throw keyword while
instantiating and exception object like throw new Exception("msg");

echo "hello";//hello
try{
$fileObj=fopen("abc.txt","r");
}catch(Exception $ex){
echo "Unable to read the file. Contact server admin";
}finally{
$fclose();
}
echo "Hello again";//

///////// MySQLi//////////////

PHP provides inbuild functionality to communicate with databases. PHP provides some
important predfined functions to connect db, to execute query, to fetch data from
db and so on. MySQLi is an improved version of mysql functionality intoduced after
php 4x and it is an object oriented implementation of MYSQL.

The MYSQL is a database client to store long term data in tabular format. In PHP
mysqli is the class which is responsible to commmunicate with mysql db client, it
requires host, username, password and/or dbname

important steps to perform db activities in php :-

1) $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);


2) check if mysqli object is created or not
if($mysqli->connect_errno) {
echo "Unable to connect db".$mysqli->connect_error
}else{
$sql = "INSERT INTO tutorials_tbl "."(tutorial_title,tutorial_author,
submission_date) "."VALUES
"."('$tutorial_title','$tutorial_author','$submission_date')";
if ($mysqli->query($sql)) {
printf("Record inserted successfully.<br />");
}else{
printf("Could not insert record into table: %s<br />", $mysqli→error);
}
}
3) msqli->close();
4) After the execution of select query, we need to check how many rows are returned
from the script. To get the count of queried result, $result->num_rows is used.
Then iterate the loop based on row counts
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
printf("Id: %s, Title: %s, Author: %s, Date: %d <br />",
$row["tutorial_id"], $row["tutorial_title"], $row["tutorial_author"],
$row["submission_date"]);
}
}

$result->fetch_assoc(), will return single row as an associative array, which will


contains the records in key_value pairs.

////////////////OOP////////////////////

Class :- The class is a blueprint or template of our program, it contains all the
required methods and properties, which can be re-use in another program. It can
have multiple objects.

Example :-

class Car{
$wheels=4;
$doors=4;
$color="White";
function drive(){
echo "Used to drive";
}
function brake(){
echo "car stop";
}
function start(){
echo "car start";
}
}

$nano=new Car();
$nano->drive;
$nano->brake;
$nano->start;
echo $nano->$wheels
echo $nano->$doors

Objects :- The objects are nothing but the example of a class. Which is mainly use
to access properties of particular class. To create an object of particular class,
instantiation is require. The instantiation is a process to create an object by
using new keyword.

Constructor :- The constructor is a type of function, that invoke at the time of


instantiation of an object. It does not required explicit call, the php script will
call it implicitly. To define constructor in php, need to use double underscore(__)
with construct word. The constructor can have multiple parameters which can be
helpful to assign values while creating an object of class.

Destructor :- The destructor is another type of function like constructor, but it


invokes implicitly, when the php script execution is about to complete. Just like
constructor, we can define destructor by using (__) with destruct word.

Access modifiers :- The access modifiers are mainly use to provide access level of
class properties.
1) public :- which can be accessible from any where outside the class.
2) protected :- can be accessible inside the class and derived class or inherited
class.
3) private :- can accessible only inside the class.

Inheritance :- The mechanism to access the properties of parent class into child
class without having an existing object. The extends keyword is use to inherit the
class. The class before extend keyword is known as child class and the class after
extends keyword can be call as parent class. It is mainly promote the re-usability
of existing code.

example :-

class car{
function drive(){
echo "car drive";
}
}

class tata extends car{


function drive(){
echo "An overrided method";
}
function start(){
echo "tata start";
}
}

Note :- When the constructor of child class is invoke, it will internally invoke
the contructor of parent class.

Overriding :- If we have multiple functions with same signature in parent and child
class, the mechanism is called overriding. When the object is created from the
child class, so the overrided method will invoke.

final :- The final keyword is use to prevent from class inheritance or to prevent
method overriding.

Constants :- Constants cannot be changed once it is declared. Class constants can


be useful if you need to define some constant data within a class. A class constant
is declared inside a class with the const keyword. We can access a constant from
outside the class by using the class name followed by the scope resolution operator
(::) followed by the constant name. Or, we can access a constant from inside the
class by using the self keyword followed by the scope resolution operator (::)
followed by the constant name.

You might also like