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()