You are on page 1of 32

What is AJAX?

AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages.
AJAX stands for Asynchronous JavaScript And XML. Any server side technology that supports JavaScript also supports AJAX. AJAX is a browser technology, and is therefore independent of web server platforms.

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change. Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs. If we are using PHP or any server side technology and need to extract data from storage on a server (eg a database or a file), we will have to make an HTTP request (either POST or GET) to get the data. Once the data is received the the web page will need to be reloaded to show the data. Using AJAX technology we can request and receive the data from server in background and then display it on the page without a reload. AJAX uses HTTP requests for this. With AJAX, JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object (XML over HTTP). With an HTTP request, a web page can make a request to, and get a response from a web server without reloading the page.

DDL Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:
o o o o o o

CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed COMMENT - add comments to the data dictionary RENAME - rename an object

DML

DML DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and update data in database. (DML) statements are used for managing data within schema objects.
Some examples:
o o

SELECT - retrieve data from the a database INSERT - insert data into a table

o o o o o o

UPDATE - updates existing data within a table DELETE - deletes all records from a table, the space for the records remain MERGE - UPSERT operation (insert or update) CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to data LOCK TABLE - control concurrency

DCL Data Control Language (DCL) statements. Some examples:


o o

GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT command

TCL Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
o o o o

COMMIT - save work done SAVEPOINT - identify a point in a transaction to which you can later roll back ROLLBACK - restore database to original since the last COMMIT SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Magic functions
Whenever you see a function name start with a double-underscore, it is a "magic" function - one that PHP has provided that you have not declared yourself. PHP reserves all functions starting with __ as magic, which means while you can use them yourself, you may find that a later version of PHP uses them as a magic function, and causes conflict. So far we've seen __sleep(), __wakeup(), __clone(), __construct(), and __destruct() - functions that give you special control over your objects that you would not otherwise be able to have. In order to have a full understanding of OOP in PHP there are five more functions you should know about: __autoload(), __get(), __set(), __call(), and __toString(). The Autoload Magic Function in PHP 5 (Page 1 of 4 ) So, if youre a PHP developer who wants to learn how to put these magic functions to work for you in a truly effortless way, or wishes to fill some gaps in your knowledge regarding their correct implementation and usage, then this group of tutorials might be the guide that youre looking for.

And now that you know what to expect from this series of articles, its to review the topics that were discussed in the last one. In that particular tutorial I discussed the implementation of a destructor method within a simple class, via the pertinent __destruct() function. In that particular situation, this function was provided with the capability to save an instance of the originating class to a session variable before being destroyed by the PHP interpreter. In doing so, I hopefully demonstrated how to create basic persistent objects by using only a simple destructor. Not too bad at all, right? However, PHP 5 offers yet another magic method that can be extremely useful for loading classes automatically, without having to use explicitly any include()/include_once() or require()/require_once() function. As the articles title suggests, Im talking specifically about the __autoload() function, which deserves a deeper analysis. Therefore, this last chapter of the series will be focused exclusively on exploring the potential of this function for including classes in a transparent way. So, jump ahead and start learning more about it!

Create an Upload-File Form


To allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files: <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Notice the following about the HTML form above:

The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.

Create The Upload Script


The "upload_file.php" file contains the code for uploading a file: <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> By using the global PHP $_FILES array you can upload files from a client computer to the remote server. The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:

$_FILES["file"]["name"] - the name of the uploaded file $_FILES["file"]["type"] - the type of the uploaded file $_FILES["file"]["size"] - the size in bytes of the uploaded file $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server $_FILES["file"]["error"] - the error code resulting from the file upload

This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.

Restrictions on Upload
In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:

<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?> Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.

Saving the Uploaded File


The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server. The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location: <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; }

else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> The script above checks if the file already exists, if it does not, it copies the file to the specified folder. Note: This example saves the file to a new folder called "upload"
MCQ Questions: 1. What does PHP stands for? Ans: Pre Hypertext Process 2. Valid ways to end a php code block: <% %> <! !> <?php ?> Invalid way: <? ?> 3. The identity operator === compare two values by: It converts them to a common compatible data type and then compares the resulting values 4. C. Under what circumstances is it possible to assign a default value to a parameter while declaring a function: When the parameter is declared as pass by reference 5. Var always starts with a $ sign

6. 7. PHP is an open source software: True 8. Valid php code: a. $_10 b. ${My Var} c. &$something d. $aVaR Wrong php code: $10_something (cant start with integer) 9. Diff bet print() and echo(): a. print will return a value to let you know if the statement worked or not, whereas trying the same thing with echo will result in error:
<?php echo "Please don't make me say it... \n"; $you = print "Hello World!\n"; echo "Returning $you"; //Returning 1, assuming the print worked ?>

b. One difference is that echo() can take multiple expressions:


<?php echo "The first", "the second"; ?>

Print cannot take multiple expressions. That is the main distinct difference! c. Print() can be used as a part of an expression, while echo() cant
1. a. b. c. d. A script is a Program or sequence of instructions that is interpreted or carried out by processor directly Program or sequence of instruction that is interpreted or carried out by another program Program or sequence of instruction that is interpreted or carried out by web server only None of above

3. a.

When compared to the compiled program, scripts run Faster

b. c. d.

Slower The execution speed is similar All of above

4. PHP is a widely used . scripting language that is especially suited for web development and can be embedded into html a. b. c. d. Open source general purpose Proprietary general purpose Open source special purpose Proprietary special purpose

5. a. b. c. d.

Which of the following is not true? PHP can be used to develop web applications. PHP makes a website dynamic. PHP applications can not be compiled. PHP can not be embedded into html.

6. a. b. c. d.

The most portable version of PHP tag that is compatible to embed in XML or XHTML too is: <? ?> <script language=php> </script> <% %>

7. a. b. c. d.

Which of the following variables is not a predefined variable? $get $ask $request $post

8. a. b. c. d.

You can define a constant by using the define() function. Once a constant is defined It can never be changed or undefined It can never be changed but can be undefined It can be changed but can not be undefined It can be changed and can be undefined

9. The following piece of script will output: <? $email=admin@psexam.com; $new=strstr($email, @ print $new; ?> a. b. c. d. admin admin@psexam @psexam.com psexam.com ;

10. Which of the following function returns the number of characters in a string variable? a. b. c. d. count($variable) len($variable) strcount($variable) strlen($variable)

11. When you need to obtain the ASCII value of a character which of the following function you apply in PHP? a. b. c. d. chr( ); asc( ); ord( ); val( );

12. A variable $word is set to HELLO WORLD, which of the following script returns in title case? a. b. c. d. echo ucwords($word) echo ucwords(strtolower($word) echo ucfirst($word) echo ucfirst(strtolower($word)

13. The difference between include() and require() a. b. c. are different how they handle failure both are same in every aspects is include() produced a Fatal Error while require results in a Warning

d.

none of above

14. When a file is included the code it contains, behave for variable scope of the line on which the include occurs a. Any variable available at that line in the calling file will be available within the called file from that point b. c. d. Any variable available at that line in the calling file will not be available within the called file Variables are local in both called and calling files None of above

15. Which of the following method sends input to a script via a URL? a. b. c. d. Get Post Both None

16. Which of the following method is suitable when you need to send larger form submissions? a. b. c. d. Get Post Both Get and Post There is no direct way for larger form. You need to store them in a file and retrieve

17. Which of the following mode of fopen() function opens a file only for writing. If a file with that name does not exist, attempts to create anew file. If the file exist, place the file pointer at the end of the file after all other data. a. b. c. d. W W+ A A+

18. The function setcookie( ) is used to a. b. c. d. Enable or disable cookie support Declare cookie variables Store data in cookie variable All of above

19. To work with remote files in PHP you need to enable a. b. c. d. allow_url_fopen allow_remote_files both of above none of above

20. fopen($file_doc,r+ a. b. c. reading writing none of above

opens a file for

d.

both of above

Answers:

1. b.

A script is a Program or sequence of instruction that is interpreted or carried out by another program

3. b.

When compared to the compiled program, scripts run Slower

4. PHP is a widely used . scripting language that is especially suited for web development and can be embedded into html a. Open source general purpose

5. d.

Which of the following is not true? PHP can not be embedded into html.

6.

The most portable version of PHP tag that is compatible to embed in XML or XHTML too is:

d.

7. b.

Which of the following variables is not a predefined variable? $ask

8. a.

You can define a constant by using the define() function. Once a constant is defined It can never be changed or undefined

9. The following piece of script will output: <? $email=admin@psexam.com; $new=strstr($email, @ print $new; ?> c. @psexam.com ;

10. Which of the following function returns the number of characters in a string variable? d. strlen($variable)

11. When you need to obtain the ASCII value of a character which of the following function you apply in PHP? c. ord( );

12. A variable $word is set to HELLO WORLD, which of the following script returns in title case? b. echo ucwords(strtolower($word)

13. The difference between include() and require() a. are different how they handle failure

14. When a file is included the code it contains, behave for variable scope of the line on which the include occurs a. Any variable available at that line in the calling file will be available within the called file from that point

15. Which of the following method sends input to a script via a URL? a. Get

16. Which of the following method is suitable when you need to send larger form submissions? b. Post

17. Which of the following mode of fopen() function opens a file only for writing. If a file with that name does not exist, attempts to create anew file. If the file exist, place the file pointer at the end of the file after all other data. c. A

18. The function setcookie( ) is used to c. Store data in cookie variable

19. To work with remote files in PHP you need to enable a. allow_url_fopen

20. fopen($file_doc,r+ d. both of above

opens a file for

his set contains 20 objective questions with 4 options each. The correct answer is provided at the end of the page.

1. In mail($param2, $param2, $param3, $param4), the $param2 contains: a. b. c. d. The message The recipient The header The subject

2. mysql_connect( ) does not take following parameter a. b. c. d. database host user ID password database name

3. Study following steps and determine the correct order (1) Open a connection to MySql server (2) Execute the SQL query (3) Fetch the data from query (4) Select database (5) Close Connection a. 1, 4, 2, 3, 5 b. 4, 1, 2, 3, 5 c. 1, 5, 4, 2, 1

d. 4, 1, 3, 2, 5

4. Which of the following is not a session function? a. b. c. d. sssion_decode session_destroy session_id session_pw

5. When uploading a file if the UPLOAD_ERR-OK contains value 0 it means a. b. c. d. Uplaod is not successful, error occurred The file uploaded with success Uploaded file size is 0 File upload progress is 0% completed

6. a. b. c. d.

Which of the following delimiter syntax is PHP's default delimiter syntax <? php ?> <% %> <? ?>

<script language="php"> </script>

7.

Which of the following statement produce different output

a. b. c. d.

<?echo "This is php example"; ?> <P="This is php example"; ?> <?PHP echo "This is php example"; php?> <script language="php"> print "This is php example";</script>

8. a. b. c. d.

Which of the following delimiter is ASP style?

<% %> <? ?> <script language="php"> </script>

9. a. b. c. d.

Php supports all four different ways of delimiting. In this context identify the false statement You can use any of the delimiting style You can use different delimiting styles in same page You can use any delimiting style but must use a single style consistently for a page Variables declared in previous blocks are remenbered on later blocks too!

10. a. b. c. d.

Which of following commenting is supported by Php Single line c++ syntax - // Shell syntax - # Both of above None of above

11. To produce the output6 I love the summer time, Which of the ofllowing statement should be used? a. b. c. d. <? Php print ("<P> I love the summer time</p>" ;?>

<? Php $ season="summer time"; print"<p> I love the $ season</p>"; ?> <?Php $ message="<p> I love the summer time </p>; ecdho $ message; ?> All of above

12. a. b. c. d.

Which of following function return 1 when output is successful? echo ( ) print ( ) both None

13. Which of followng statement is more suitable if you want to output a blend of static text and dynamic information stored within one or several variables? a. b. c. d. echo ( ) print ( ) Print f ( ) None of above

14.

Which of the following type specifier is invalid in print f ( ) functions

a. b. c. d.

%a %b %c %d

15. Which of the following function can assign the output to a string variable a. b. c. d. echo ( ) print ( ) print f ( ) s print f ( )

16. Which of the following data type is not seal or datetype supported by PHP a. b. c. d. Array String Float Boolean

17. Which of the following data type is compound datatype supported by PHP a. b. c. d. Array String Float Boolean

18. If a boolean variable $ alive= 5; a. b. c. d. $ alive is false $ alive is true $ alive is overflow the statement is snot valid

19. For integer data type PHP 6 introduced a. b. c. d. 8 bit integer value 16 bit integer value 32 bit integer value 64 bit integer value

20. For integer data type PHP 5 and earlier supported a. b. c. d. 8 bit integer value 16 bit integer value 32 bit integer value 64 bit integer value

Answers:

1. In mail($param2, $param2, $param3, $param4), the $param2 contains: d. The subject

2. mysql_connect( ) does not take following parameter d. database name

3. Study following steps and determine the correct order (1) Open a connection to MySql server (2) Execute the SQL query (3) Fetch the data from query (4) Select database (5) Close Connection a. 1, 4, 2, 3, 5

4. Which of the following is not a session function? d. session_pw

5. When uploading a file if the UPLOAD_ERR-OK contains value 0 it means b. The file uploaded with success

6. a.

Which of the following delimiter syntax is PHP's default delimiter syntax

7. c.

Which of the following statement is invalid? <?PHP echo "This is php example"; php?>

8. b.

Which of the following delimiter is ASP style? <% %>

9. c.

Php supports all four different ways of delimiting. In this context identify the false statement You can use any delimiting style but must use a single style consistently for a page

10. c.

Which of following commenting is supported by Php Both of above

11. To produce the output "I love the summer time", Which of the ofllowing statement should be used? d. All of above

12. b.

Which of following function return 1 when output is successful? print ( )

13. Which of followng statement is more suitable if you want to output a blend of static text and dynamic information stored within one or several variables? c. Printf ( )

14. a.

Which of the following type specifier is invalid in print f ( ) functions %a

15. Which of the following function can assign the output to a string variable d. sprintf ( )

16. Which of the following data type is not seal or datetype supported by PHP a. Array

17. Which of the following data type is compound datatype supported by PHP a. Array

18. If a boolean variable $ alive= 5; b. $ alive is true

19. For integer data type PHP 6 introduced d. 64 bit integer value

20. For integer data type PHP 5 and earlier supported c. 32 bit integer value

1. Trace the odd data type a. floats b. integer c. doubles

d. Real number 2. Which of the folowing are valid float values? a. 4.5678 b. 4.0 c. 7e4 d. All of above 3. In php string data are a. delimited by single quote b. delimited by double quote c. delimited by <<< identifier d. All of above 4. Which of the following delimiting method is known as string Interpolation a. delimited by single quote b. delimited by double quote c. delimited by <<< identifier d. All of above 5. Which datatypes are treaded as arrays a. Integer b. Float c. String d. Booleans 6. Which of following are compound data type? a. Array

b. Objects c. Both d. None 7. Casting operator introduced in PHP 6 is a. (array) b. (int64) c. (real) or (double) or (float) d. (object) 8. When defining identifier in PHP you should remember that a. Identifier are case sensitive. So $result is different than $ result b. Identifiers can be any length c. Both of above d. None of above 9. Identify the invalid identifier a. my-function b. size c. some word d. This&that 10. Which of folowiing variable assignment is 'by value' assignment in PHP a. $value1= $value? b. $value1= & $value? c. $value1= & $value? d. None

11. Identify the variable scope that is not supported by PHP a. Local variables b. Function parameters c. Hidden variables d. Global variables 12. The output of ofllowing script would be $somerar=15; function ad it () { GLOBAL $somevar; $somerar++ ; echo "somerar is $somerar"; } addit (); a. somerar is 15 b. somerar is 16 c. somerar is 1 d. somerar is $ somerar 13. Variable scope on which a variable does not loose its value when the function exists and use that value if the function is called again is: a. Local b. function parameter c. static d. None of above 14. The left association operator % is used in PHP for

a. percentage b. bitwise or c. division d. modulus 15. The left associative dot operator (.) is used in PHP for a. multiplication b. concatenation c. separate object and its member d. delimeter 16. Trace the false statement a. Any code found within an included file will inherit the variable scope of the location of its caller b. Because the included code will be embedded in a PHP execution block, the PHP execution block, the PHP escape tags (<?php?> aren't required on the file to be included c. For the inclusion of remote files the allow-url-pope must be enabled ad URL wrapper must be supported d. Including a file produces the same result as copying the data from the file specified into the location in which the statement appears. 17. Which of the following functions require the allow-url-fopen must be enabled? a. include() b. require() c. both of above d. None of above 18. Which function includes the specified file even the statement evaluates to false in which block the function is placed. a. include ()

b. require () c. both of above d. None of above 19. On failure of which statement the script execution stops displaying error/warning message? a. rinclude () b. require () c. both of above d. None of above 20. Trace the function that does continue the script execution even if the file inclusion fails a. include () b. require () c. both of above d. None of above

Answers:

1. Trace the odd data type b. integer

2. Which of the folowing are valid float values? d. All of above 3. In php string data are d. All of above 4. Which of the following delimiting method is known as string Interpolation c. delimited by <<< identifier 5. Which datatypes are treaded as arrays c. String 6. Which of following are compound data type? c. Both 7. Casting operator introduced in PHP 6 is b. (int64) 8. When defining identifier in PHP you should remember that c. Both of above 9. Identify the invalid identifier d. This&that 10. Which of folowiing variable assignment is 'by value' assignment in PHP a. $value1= $value2 11. Identify the variable scope that is not supported by PHP c. Hidden variables 12. The output of ofllowing script would be $somerar=15; function ad it () {

GLOBAL $somevar; $somerar++ ; echo "somerar is $somerar"; } addit (); b. somerar is 16 13. Variable scope on which a variable does not loose its value when the function exists and use that value if the function is called again is: c. static 14. The left association operator % is used in PHP for d. modulus 15. The left associative dot operator (.) is used in PHP for b. concatenation 16. Trace the false statement b. Because the included code will be embedded in a PHP execution block, the PHP execution block, the PHP escape tags (<?php?> aren't required on the file to be included 17. Which of the following functions require the allow-url-fopen must be enabled? c. both of above 18. Which function includes the specified file even the statement evaluates to false in which block the function is placed. b. require () 19. On failure of which statement the script execution stops displaying error/warning message? b. require () 20. Trace the function that does continue the script execution even if the file inclusion fails a. include ()

You might also like