Professional Documents
Culture Documents
It is written inside <?php ?> and we can write multiple time inside
that starting and closing bracket.
The file name is store in .php format and we can also write html
inside php file.
Variable:
Always start with a $
And after that all same as c++
Values a variable can store :
Integer ,String, Float ,Boolean ,Array ,Object and Null
Local and Global
$ab = "hello";
function a(){
$ab = "hi";
echo $ab;
}
function b(){
echo $ab;
}
a();
b();
echo $ab;
output:
<h1>hi<br />
<b>Warning</b>: Undefined variable $ab in <b>C:\xampp\htdocs\
project\hello.php</b> on line <b>45</b><br />
hello</h1>
Global variable cannot be access inside a function i.e if we print a
variable with same name as global then it will so as undefined as
above.
Php
But we can access global variable by using global key word i.e global
$name but if we change that inside function change will affect
original also.
Global variables are store in a built in associative array $GLOBALS as
associative pair i.e key value pair and we can also access global
variable as $GLOBALS[‘name’] = value; .
A superglobal variable is a variable that is always available in all scopes, that is, it can be
accessed from anywhere, whether from outside a function or inside.
Define as $_nameallcapital;
Operator :
Arithmetic operators ( + , - , * , / , % , ( ** exponential ) )
Assignment operators ( = ,+= , -= , *= , /= , %= ,**=)
Increment/Decrement operators (unary operator) ( ++ , --
)
Comparison operators ( == ,!= , < , <= , > , >= )
Logical operators ( && , || ,! )
Conditional Statement:
1. if
if(condition){}
2. if – else
if(condition){}
else{}
3. if - elseif – else //no space between elseif
if(condition){}
Php
elseif(condition){}
else{}
4. switch
switch($any){
case “1”:
statement;
break;
case”2”:
statement;
break;
.
.
.
Default:
Statement;
Break;}
Loop :
1. while
while(condition){}
2. do-while
do{
}while(condition);
3. for
for(initialization ; condition ; iteration){
statement;
}
Php
4. foreach
foreach($array as $element){
statement;
}
Arrays :
1. Numeric Indexed Array
$names = array(val1,val2,…);
$names = array(“val1”,”val2”);
foreach($names as $name){}
2. Associative Array
$names=array(“key1”=>value1,”key2”=>value2);
$names=array(“key1”=>”value1,”key2”=>value2);
foreach($names as $key=>$value){}
3. Multidimensional Array
4. $students = array(
5. "amit"=>array(
6. "phy"=>87,
7. "chem"=>96,
8. "math"=>43
9. ),
10. "rohit"=>array(
11. "phy"=>85,
12. "chem"=>76,
13. "math"=>93
14. ),
15. "alok"=>array(
16. "phy"=>95,
17. "chem"=>66,
18. "math"=>73
19. )
20. );
21. foreach($students as $student=>$arr){
22. foreach($arr as $sub=>$mark){
23. echo $student." scored ".$mark." mark in ".$sub."\n";
Php
24. }
25. }
26.
Strings:
All the given statements are true. The single quotes treat a string exactly as it is. The
double quotes replace any variable within it (if any) with the value it holds. The double
quotes allow many special escaped characters to be used.
Escape character either adds a special meaning to the following character or if the following
character is already special, the escape character removes its special meaning and treats it
like a normal text character.
Define a string using ‘ quote will print the entire string as such
without any special meaning
Php
https://www.php.net/manual/en/ref.strings.php
Function :
All the given statements are true. A function is a block of code that has a name. A
function has two parts: a function definition and a function call. A function is defined when a
piece of code might be used at several places.
Fun-defination:
function fun-name(fun-arguments i.e $a,$b){code
or body}
default : function name($a ,$b=0){}
function always return a value at onetime.
Php
Fun-call:
$var = fun-name(arguments i.e 1,2);
Call by reference using & before variable name
Form submission:
The action attribute defines the url to which the new HTTP request will be initiated
with the form data.
The default value for the form method attribute is get. If we do not provide the
method attribute to a form, it will by default assume it to be get.
On form submission, a new request is always initiated. The form method attribute
defines the request method type for the new HTTP request.
In the case of the GET method, all the form data entered by the user gets added at
the end of the new url, whereas in the case of the POST method, nothing happens. The
POST method is not used for all form submissions, we do use the GET method for some
type of form submissions. The form data that goes along the HTTP request can be accessed
in PHP irrespective of the request method type.
For the GET request method, we can access form data in PHP using $_GET. For
the POST request method, we can access form data in PHP using $_POST.
We can access the form data in the PHP file that is being passed in an HTTP request
of get method type using $_GET variable. Search forms all across particularly use the GET
method
Cookies :
All the given statements are true. A cookie is a small piece of information
stored on the browser of a user by a website. Cookies are website specific,
that is, every website that a user visits has its own set of cookies stored on
the user's browser. Once cookies are stored on a browser, they are sent
along every HTTP request for that website.
setcookie("user_email",$row['email'],time()+3600);
setcookie("name",$row['name'],time()+3600);
$user_email=$_COOKIE['user_email'];
$name = $_COOKIE['name'];
Sessions:
Php
All the given statements are true. Sessions are a small piece of information
stored on the server. Sessions are user specific, that is, for every user we
have a separate session and store data for that user in that session. Each of
the sessions has a unique session id associated with it, through which these
sessions can be accessed.
session_start();
The session_destroy() function deletes the existing data in a session for the
logged in user.
session_destroy();
$_SESSION['user_email'] = $row['email'];
$_SESSION['name'] = $row['name'];
$user_email=$_SESSION['user_email'];
$name = $_SESSION['name'];