PHP Flashcards
PHP
- Hypertext preprocessor
- widely used open source scripting language
- PHP Scripts are executed on server
php file
- Contains text ,html, css, js and php.
- Pagep is executedon server and result is executed to browser as plain html
- Extension .php
ADV of PHP
- PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
- PHP is compatible with almost all servers used today (Apache, IIS, etc.)
- PHP supports a wide range of databases
- PHP is free. Download it from the official PHP resource: www.php.net
- PHP is easy to learn and runs efficiently on the server side
features of php
- PHP can generate dynamic page content
- PHP can create, open, read, write, delete, and close files on the server
- PHP can collect form data
- PHP can send and receive cookies
- PHP can add, delete, modify data in your database
- PHP can be used to control user-access
- PHP can encrypt data
basic syn of php
<?php
|
|
?> is syn
echo
Predefined function that is used to output the text on web page
Other array functions
- array()
- array_change_key_case_()
- array_chunks()
- array_search()
- array_reverse()
- sorting(6)
array()
creates and returns an array
syn=arrayname=array(values_ _)
array_change_key_case()
Changes the case of the all keys
syn: array_change_key_case( $arrayname, case_lower)
array_chunks()
Divides the array into smaller chunks
syn: array_chunks($arrayname_,no))
array_reverse()
function returns an array containing elements in reversed order
syn: array_reverse($arrayname)
array_search()
searches the specified value in an array. It returns key if search is successful
syn: array_search(“valuetosearch”,$arrayname)
sorting array functions
- sort()
- rsort()
- asort()
- arsort()
- aksort()
- krsort()
php functions
1.piece of the code that can be reused many times.
2.takes input as args and returns the value.
Advantages:
1. code reusability
2. less code
3. easy to understand
function declaration
function functionname()
{
//code to execute
}
php function arguments
-supports :
1. call-by value
2. call-by reference
3. default args,
4. variable-length
(Same as py)
call by value
- functionname(“value”)
call by reference
1.“&” is used while declaring variables in the fun
2. we use another variable to collect the value
syn ++
function funcname(&$_ _ _)
{
//code
}
obj=value
funname($function name)
parametrized function
- Functions with parameters
- You can pass any number of parameters in a function
- These passed parameters act as variables inside the function
- These are specified inside parenthesis after the function name
- Output depends upon the dynamic values passed as the parameters into function
Cookie
- small piece if info which is stored at client browser.
- used to recogonize the user
- cookie is created at client browser, each time when client sends request to the server, cookie is embedded with req—such that cookie can be recieved at the server side.
- cookie can be created, sent and recieved at server end.
session
- used to store and pass info from one page to another temporarily(until user close the website)
- php session technique is widely used in shopping where we need to store and pass cart information(eg: username, product code, product name, product price, etc)f om one page to another.
3.php creates unique user id for each browser to recogonize the user and avoid conflict between multiple browsers.
session functions
1.session_start(): Starts in new or resumes session if session is created already session_start();
2.$SESSION: associative array that contains all session variables–used to set and get session vari values
store info: $session[“user”] =”value”
to get info: echo $SESSION[“user”];
3. destroying session: Destroy all session variables completely —session_destroy()
session eg
File: session1.php
1. <?php
2. session_start();
3. ?>
4. <html>
5. <body>
6. <?php
7. $_SESSION[“user”] = “Sachin”;
8. echo “Session information are set successfully.<br></br>”;
9. ?>
10. <a>Visit next page</a>
11. </body>
12. </html>
Save this code with session2.php
13. ?php
14. session_start();
15. ?>
16. <html>
17. <body>
18. <?php
19. echo “User is: “.$_SESSION[“user”];
20. ?>
21. </body>
22. </html>
Math functions
Provides many predefined math functions in constants that can be used to perform mathematical operations
1. abs()
2. ceil()
3. floor()
4. sqrt()
regular expr(pattern search)
- Also called as rejex
-
Pattern or a sequence of characters which describe a special search pattern as text string
functions : - Allows you to search specific string inside another string
- Replace one string by another
- Split strings into multiple chunks
-uses arth operators (+,-,^)To create complex expressions
- By default these are case sensitive
adv of regex
- Helps programmers to validate text strings
- Offers a powerful tool to analyse and search a pattern as well as modify the text str
- By using rejects functions we can get simple and easy solutions to identify patterns
- Helps creating html template system recognition tags
- Widely used for browser detection, Firm validation spam filtration and password strength checking
- helpful in user input validation testing like email address mobile number and ip address
- Helps in highlighting the special keywords in file based upon the search result or input
- Meta characters allow us to create more complex patterns
file inclusion
PHP allows us to create various elements and functions which are used same *several times in many pages this takes so much time to script therefore file inclusion topic is introduced
def: File inclusion helps to include files in various programmes and saves the effort of writing code multiple times
- When you want to apply the same html or php code to multiple pages of website page inclusion is used
types of page inclusion
- include–creates a warning(E_WARNING) and continue the execution of script
- require– Generates a fatal error (E_COMPILE_ERROR) and stops the execution
advantages (2)
- code reusability
- easy ediatability
include and eg
Used to include a file on the basis of given path(relative/ abs path os a file)
Syn: —include “filename”; or
—include (“filename”);
eg: File: menu.html
<a>Home</a> |
<a>PHP</a> |
<a>Java</a> |
<a>HTML</a>
File: include1.php
<?php include(“menu.html”); ?>
<h1>This is Main Page</h1>
require
Very similar to include along with the syntax and the programme the only difference is Stop execution of script if the file is not found whereas include doesn’t
Syn: —require “filename”; or
—require (“filename”);
eg:File: menu.html
<a>Home</a> |
<a>PHP</a> |
<a>Java</a> |
<a>HTML</a>
File: require1.php
<?php require(“menu.html”); ?>
<h1>This is Main Page</h1>
PHP include vs PHP require
Both include and require are same. But if the file is missing or inclusion
fails, include allows the script to continue but require halts the script producing a fatal
E_COMPILE_ERROR level error.
Let’s understand the difference with the help of example:
Example
include.php
1. <?php
2. //include welcome.php file
3. include(“welcome.php”);
4. echo “The welcome file is included.”;
5. ?>
Output:
The welcome.php file is not available in the same directory, which we have included. So,
it will produce a warning about that missing file but also display the output.
Warning: include(welcome.php): failed to open stream: No such file or directory
in C:\xampp\htdocs\program\include.php on line 3
Warning: include(): Failed opening ‘welcome.php’ for inclusion
(include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\program\include.php on
line 3
The welcome file is included.
require.php
1. <?php
2. echo “HELLO”;
3. //require welcome.php file
4. require(“welcome.php”);
5. echo “The welcome file is required.”;
6. ?>
Output:
In case of require() if the file (welcome.php) is not found in the same directory. The
require() will generate a fatal error and stop the execution of the script, as you can see
in the below output.
HELLO
Warning: require(Welcome.php): failed to open stream: No such file or directory
in C:\xampp\htdocs\program\include.php on line 3
Fatal error: require(): Failed opening required ‘Welcome.php’ (include_path
.
file handling
php file system allows us to create file, read file line by line, read file character by character, write file, append file, delete file and Close file
1. fopen(“path”,”mode”)
2. fclose($handle)
3. string fread( resource $handle, int $length)
4. fwrite($filename, value)
5. unlink(filename, value)—del