You are on page 1of 10

Php

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.

Escape character: \n , \t etc \

Define a string using “ double quote will include special


meaning i.e $names will give the variable value and if include \
back of a special meaning character it will exclude it special
meaning i.e \$names will show $names not its value

Define a string using ‘ quote will print the entire string as such
without any special meaning
Php

Concatenation (.) will concatenate 2 strings


$str = "hello";
    echo "hii $str! \n";
    echo "hii \$str! \n";
    echo "hii \"$str!\" \n";
    echo 'hii $str! \n';
    echo "2nd line";
output:
<h1>hii hello!
hii $str!
hii "hello!"
hii $str! \n2nd line</h1>

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.

<form method = "post" action = "login_submit.php">


Php

       Email : <input type="text" name="email"/> <br>


       Password : <input type="password" name="password"/> <br/>
       
        <input type="submit" name="submit" value="Submit"/>    
    </form>
<?php
$Email = $_POST['email'];
$Password = $_POST['password'];
echo "$Email \n$Password";
?>

Database Related Function:


Php

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

The mysqli_connect() takes four arguments: Database hostname, Database


username, Database password, Database name - in this order only. The function returns a
reference object which represents the connection to the MySQL Server. It returns FALSE if
for some reason it fails to establish a connection.
The mysqli_connect_error() function returns the last error message from the last call
to mysqli_connect().
The function mysqli_query() returns FALSE if the query fails for some reason. When
the query executes successfully, it returns TRUE for most cases. But for SELECT queries, it
returns a reference object, through which we can access the fetched data. The
mysqli_query() function is used to execute an SQL query on the connected database
The function fetches the data from the reference object that we get from the
mysqli_query() function in the case of SELECT queries. The function fetches the data row-
wise, that is, one row at a time. As it fetches the first result row, it moves the internal pointer
to point to the next row, so that next time when we call the function it fetches us the second
result row. When there are no more rows left to fetch, the function returns FALSE.
The function mysqli_error() returns the last error message for the last call to
mysqli_query().

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'];

setcookie() function is used to store a cookie on the user's browser.

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.

Login to a website means a user is properly identified in an authenticated


way.
Session_start : the function expects a cookie with name PHPSESSID to have
come along with HTTP request ,the function fetches the associated session
using the session id it gets from the cookie ,the function assigns the fetched
session to a superglobal variable called %_SESSION.

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'];

You might also like