PHP
PHP
Web Server-it is a software which serves websites on the internet, to achieve a particular goal it acts as a
middleman between the server and the client.
Internet Information Services (IIS) – It is a flexible, general-purpose web server from Microsoft that runs on
Windows systems to serve requested HTML pages or files.
Loops: While , do..while ,for , foreach
Foreach loop: The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Ex: <?php String function: 1 strlen():- Return the Length of a String
$colors = array("red", "green", "blue", "yellow"); 2 str_word_count() - Count Words in a String
foreach ($colors as $value) { 3. strrev() - Reverse a String
echo "$value <br>"; } ?> 4 strtoupper() - converts a string to uppercase
Default argument function: <?php 5 strtolower() - converts a string to lowercase
function greeting($name="GeeksforGeeks") 6 Trim() Removes whitespace or other characters from both sides
{ echo "Welcome to $name "; of a string
echo("\n"); } 7 str_replace(), strcmp(),strpos(),str_split()
greeting("Gfg"); ex: <?php
// Passing no value echo strlen(“hello world”); ?>
greeting(); Math function 1 abs() :returns absolute value of given number
greeting("A Computer Science Portal"); 2 ceil() function rounds fractions up. >3.3 >4
?> 3 floor() function rounds fractions down> 3.4>3
Array functions:1 array() function creates and 4 sqrt() function returns square root of given argument
returns an array. 5 decbin() function converts decimal number into binary
2 count() function counts all elements in an array. 6 dechex() , decoct(), bindec()
Ex: <?php 7 round() function is used to find rounds a float number.
$season=array("summer","winter","spring","autumn"); ex: echo round(3.0978645);
echo count($season); // output: 4 ?> 8 pow() , Rand() ,Max(),min(),
3 sort() function sorts all the elements in an array. Date function:
4 rsort()Sort the elements in descending getdate() , date_add(), date_create() , date_format() ,
alphabetical order checkdate(), time(),
5 array_search() searches the specified value in an array
6 array_change_key_case(),asort(),arsort(),
array_merge(),array_sum(),array_diff_key()
func_num_args() It returns the total number of arguments provided in the function call operation. For example, if
"func_num_args()" returns 1, you know that there is only 1 argument provided by calling code.
Ex: <?php
function f1()
{ $numargs = func_num_args();
echo "Number of arguments: $numargs\n"; }
f1(1, 2, 3); // Prints 'Number of arguments: 3'
?>
POST method: The POST method transfers information via HTTP headers. The example below displays a simple
HTML form with two input fields and a submit button:
GET Method: The GET method sends the encoded user information appended to the page request. The page and
the encoded information are separated by the ? character
<html> <html>
<body> <body>
<form action="welcome.php" method="post"> Welcome <?php echo $_POST["name"]; ?><br>
Name: <input type="text" name="name"><br> Your email address is: <?php echo $_POST["email"]; ?>
E-mail: <input type="text" name="email"><br> </body> </html>
<input type="submit">
</form>
</body> </html
PHP-Personal Home Page.
PHP was created by Rasmus Lerdorf in 1994.
PHP files have extension ".php"
______ function returns the number of arguments passed into user-defined function<<fun_num_arg()
_____ function is used to replace<<str_replace()
Unit-2 JSON: JavaScript Object Notation
AJAX: (Asynchronous JavaScript And XML)
GD libaray function: • imagedestroy — Destroy an image
• getimagesize — Get the size of an image
• imagearc — Draws an arc
• imagechar — Draw a character horizontally
• imagecopy — Copy part of an image
Methods to submit form: GET and POST
GD? (Graphics Draw)
• In PHP ________ superglobal variable is used to get cookie. ($_COOKIE)
• _______ function is used to set the cookie. (setcookie())
• Session variables are set with the ________ super global variable. ($_SESSION)
• A PHP session is started with the_______ function. (session_start())
• ______ function is used to destroy the session. (session_destroy())
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All
its properties and methods go inside the braces
Objects are instance of classs Classes are nothing without objects! We can create multiple objects from a class.
Each object has all the properties and methods defined in the class, but they will have different property values.
Unit-2
A cookie :is a small file that the server embeds on the user's computer. A cookie is often used to identify a user.
Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both
create and retrieve cookie values.
PHP cookie is a small piece of information which is stored at client browser. Cookie is created at server side and
saved to client browser.
Ex : setcookie(name, value, expire, path, domain, secure, httponly);
A session is a way to store information (in variables) to be used across multiple pages.
PHP session is used to store and pass information from one page to another temporarily (until user close the
website).
PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g.
username, product code, product name, product price etc from one page to another.
Ex: <?php
session_start(); ?>
// Set session variables
$_SESSION["favcolor"] = "red";
$_SESSION["favhero"] = "Swami Vivekanand";
echo "Session variables are set.";
?>
A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text,
you can use this search pattern to describe what you are searching for. A regular expression can be a single
character, or a more complicated pattern. Regular expressions help you accomplish tasks such as validating email
addresses, IP address etc.
• POSIX Regular Expressions
Brackets:Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to
find a range of characters
Quantifiers :The frequency or position of bracketed character sequences and single characters can be denoted by a
special character. The +, *, ?, {int. range}, and $ flags all follow a character sequence.
• PERL Style Regular Expressions
ereg() The ereg() function searches a string specified by string for a string specified by pattern, returning true if the
pattern is found, and false otherwise.
ereg_replace() The ereg_replace() function searches for string specified by pattern and replaces pattern with
replacement if found.
eregi() The eregi() function searches throughout a string specified by pattern for a string specified by string. The
search is not case sensitive
PHP json_encode: The json_encode() function returns the JSON representation of a value. In other words, it
converts PHP variable (containing array) into JSON.
Syntax: string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr); ?>
PHP json_decode:The json_decode() function decodes the JSON string. In other words, it converts JSON string into
a PHP variable.
Syntax: mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
Ex: <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true)); ?>
AJAX: AJAX is not a programming language.
AJAX just uses a combination of:
• A browser built-in XMLHttpRequest object (to request data from a web server)
• JavaScript and HTML DOM (to display or use the data)
JSON: JSON is a text format for storing and transporting data
• JSON is a lightweight data-interchange format
• JSON is plain text written in JavaScript object notation
JSON is used to send data between computers, JSON is language independent The file type for JSON files is ".json"
Unit-3
Insert data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
if($_POST){
$uname=$_POST['uname'];
$upass=$_POST['upass'];
}
$sql = "INSERT INTO users VALUES ('$uname', '$upass')";
if ($conn->query($sql) == TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<html>
<Body><form method="post">
uname:- <input type="text" name="uname"><br/>
Password:- <input type="password" name="upass"><BR/>
<input type="submit" name="submit"/>
</form>
</BODY>
</html>