Академический Документы
Профессиональный Документы
Культура Документы
CORE COURSE IX
PROGRAMMING IN PHP
Objective :
Unit I
Unit II
Unit III
Unit IV
Unit V
Text Book:
Reference Books:
*****
23
PROGRAMMING IN PHP (16SCCCA9,16SCCCS9) III BCA / B.Sc (CS)
1. What is PHP?
PHP is a recursive acronym for "PHP: Hypertext Preprocessor". PHP is a server side scripting
language that is embedded in HTML. It is used to manage dynamic content, databases,
session tracking, even build entire e-commerce sites.
2. What are the common usage of PHP?
Common uses of PHP −
PHP performs system functions, i.e. from files on a system it can create, open, read,
write, and close them.
PHP can handle forms, i.e. gather data from files, save data to a file, thru email you
can send data, return data to the user.
You add, delete, modify elements within your database thru PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your website.
It can encrypt data.
3. In how many ways you can embed PHP code in an HTML page?
All PHP code must be included inside one of the three special markup tags ate are
recognised by the PHP Parser.
Constants cannot be defined by simple assignment, they may only be defined using
the define() function.
Constants may be defined and accessed anywhere without regard to variable
scoping rules.
Once the Constants have been set, may not be redefined or undefined.
15. What are PHP magic constants?
PHP provides a large number of predefined constants to any script which it runs known as
magic constants.
16. What is the purpose of _LINE_ constant?
_LINE_ − The current line number of the file.
17. What is the purpose of _FILE_ constant?
_FILE_ − The full path and filename of the file. If used inside an include,the name of the
included file is returned. Since PHP 4.0.2, _FILE_ always contains an absolute path whereas
in older versions it contained relative path under some circumstances.
18. What is the purpose of _FUNCTION_ constant?
_FUNCTION_ − The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns
the function name as it was declared (case-sensitive). In PHP 4 its value is always
lowercased.
19. What is the purpose of _CLASS_ constant?
_CLASS_ − The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class
name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
20. What is the purpose of _METHOD_ constant?
_METHOD_ − The class method name. (Added in PHP 5.0.0) The method name is returned
as it was declared (case-sensitive).
21. What is the purpose of break statement?
break terminates the for loop or switch statement and transfers execution to the statement
immediately following the for loop or switch.
22. What is the purpose of continue statement?
continue causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
23. Explain the syntax for 'foreach' loop.
The foreach statement is used to loop through arrays. For each pass the value of the current
array element is assigned to $value and the array pointer is moved by one and in the next
pass next element will be processed.
<?php
echo strlen("Hello world!");
?>
This will produce following result −
12
29. How will you locate a string within a string in PHP?
The strpos() function is used to search for a string or character within a string. If a match is
found in the string, this function will return the position of the first match. If no match is
found, it will return FALSE. Let's see if we can find the string "world" in our string −
<?php
echo strpos("Hello world!","world");
?>
This will produce following result −
6
30. How will you get environment variables in PHP?
PHP provides a function getenv() to access the value of all the environment variables.
31. How will you get the browser's details using PHP?
One of the environemnt variables set by PHP is HTTP_USER_AGENT which identifies the
user's browser and operating system.
32. How will you generate random numbers using PHP?
The PHP rand() function is used to generate a random number. This function can generate
numbers with-in a given range. The random number generator should be seeded to prevent
a regular pattern of numbers being generated. This is achieved using the srand() function
that specifiies the seed number as its argument.
33. What is the purpse $_PHP_SELF variable?
The PHP default variable $_PHP_SELF is used for the PHP script name and when you click
"submit" button then same PHP script will be called.
34. How will you redirect a page using PHP?
The PHP header() function supplies raw HTTP headers to the browser and can be used to
redirect it to another location. The redirection script should be at the very top of the page to
prevent any other part of the page from loading. The target is specified by the Location:
header as the argument to the header() function. After calling this function the exit()
function can be used to halt parsing of rest of the code.
35. How can you display a file download dialog box using PHP?
The HTTP header will be different from the actual header where we send Content-Type as
text/html\n\n. In this case content type will be application/octet-stream and actual file name
will be concatenated alongwith it. For example,if you want make a FileName file
downloadable from a given link then its syntax will be as follows.
#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Actual File Content
open( FILE, "<FileName" );
while(read(FILE, $buffer, 100) )
{
print("$buffer");
}
36. How will you get information sent via get method in PHP?
The PHP provides $_GET associative array to access all the sent information using GET
method.
37. How will you get information sent via post method in PHP?
The PHP provides $_POST associative array to access all the sent information using POST
method.
38. What is the purpse $_REQUEST variable?
The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST
variable can be used to get the result from form data sent with both the GET and POST
methods.
39. Which function will you use to create an array?
array() − Creates an array.
40. How can you sort an array?
sort() − Sorts an array.
41. What is the difference between single quoted string and double quoted string?
Singly quoted strings are treated almost literally, whereas doubly quoted strings replace
variables with their values as well as specially interpreting certain character sequences.
<?php
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
print "<br />";
$literally = "My $variable will print!\\n";
print($literally);
?>
This will produce following result −
My $variable will not print!\n
My name will print
42. How will you concatenate two strings?
To concatenate two string variables together, use the dot (.) operator.
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
This will produce following result −
Hello World 1234
43. What is the use of $_REQUEST variable?
The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST
variable can be used to get the result from form data sent with both the GET and POST
methods.
44. How will you include the content of a PHP file into another PHP file?
There are two PHP functions which can be used to included one PHP file into another PHP
file.
The include() Function
The require() Function
45. What is the difference between include() Function and require() Function?
If there is any problem in loading a file then the require() function generates a fatal error
and halt the execution of the script whereas include() function generates a warning but the
script will continue execution.
46. How will you open a file in readonly mode?
The PHP fopen() function is used to open a file. It requires two arguments stating first the
file name and then mode in which to operate. "r" mode opens the file for reading only and
places the file pointer at the beginning of the file.
47. How will you read a file in php?
Once a file is opened using fopen() function it can be read with a function called fread(). This
function requires two arguments. These must be the file pointer and the length of the file
expressed in bytes.
53. How will you make a check that a cookie is set or not?
You can use isset() function to check if a cookie is set or not.
54. How will you delete a cookie?
To delete a cookie you should call setcookie() with the name argument only.
55. How will you start a session in PHP?
A PHP session is easily started by making a call to the session_start() function.This function
first checks if a session is already started and if none is started then it starts one. It is
recommended to put the call to session_start() at the beginning of the page.
56. How will you access session variables in PHP?
Session variables are stored in associative array called $_SESSION[]. These variables can be
accessed during lifetime of a session.
57. How will you check if session variable is already set or not in PHP?
Make use of isset() function to check if session variable is already set or not.
58. How will you unset a single session variable?
Here is the example to unset a single variable −
<?php
unset($_SESSION['counter']);
?>
59. How will you destroy the session?
A PHP session can be destroyed by session_destroy() function.
60. How will you send an email using PHP?
PHP makes use of mail() function to send an email. This function requires three mandatory
arguments that specify the recipient's email address, the subject of the the message and the
actual message additionally there are other two optional parameters.
mail( to, subject, message, headers, parameters );
61. What is the purpose of $_FILES variable in PHP?
This is a global PHP variable. This variable is an associate double dimension array and keeps
all the information related to uploaded file.
62. How will you access the uploaded file in PHP?
Using $_FILES['file']['tmp_name'] − it provides access to the uploaded file in the temporary
directory on the web server.
63. How will you access the actual name of the uploaded file in PHP?
Using $_FILES['file']['name'] − it provides the actual name of the uploaded file.
64. How will you access the size of the uploaded file in PHP?
Using $_FILES['file']['size'] − it provides the size in bytes of the uploaded file.
65. How will you access the content type of the uploaded file in PHP?
Using $_FILES['file']['type'] − it provides the MIME type of the uploaded file.
66. How will you access the error code associated with file upload in PHP?
Using $_FILES['file']['error'] − it provides the error code associated with this file upload.
67. What is the purpose of $GLOBALS variable in PHP?
$GLOBALS − Contains a reference to every variable which is currently available within the
global scope of the script. The keys of this array are the names of the global variables.
94. How will you access the reference to same object within the object in PHP?
The variable $this is a special variable and it refers to the same object ie. itself.
95. How will you create objects in PHP?
Once you defined your class, then you can create as many objects as you like of that class
type. Following is an example of how to create object using new operator.
$physics = new Books;
$maths = new Books;
$chemistry = new Books;
96. How will you call member functions of a class in PHP?
After creating your objects, you will be able to call member functions related to that object.
One member function will be able to process member variable of related object only.
Following example shows how to set title and prices for the three books by calling member
functions.
$physics−>setTitle( "Physics for High School" );
$chemistry−>setTitle( "Advanced Chemistry" );
$maths−>setTitle( "Algebra" );
$physics−>setPrice( 10 );
$chemistry−>setPrice( 15 );
$maths−>setPrice( 7 );
97. What is function overriding?
Function definitions in child classes override definitions with the same name in parent
classes. In a child class, we can modify the definition of a function inherited from parent
class.
98. What are interfaces in PHP?
Interfaces are defined to provide a common function names to the implementors. Different
implementors can implement those interfaces according to their requirements. You can say,
interfaces are skeltons which are implemented by developers.
99. What is the use of final keyword?
PHP 5 introduces the final keyword, which prevents child classes from overriding a method
by prefixing the definition with final. If the class itself is being defined final then it cannot be
extended.
10 | V a l l u v a r C o l l e g e o f S c i e n c e a n d M a n a g e m e n t , K a r u r .
PROGRAMMING IN PHP (16SCCCA9,16SCCCS9) III BCA / B.Sc (CS)
11 | V a l l u v a r C o l l e g e o f S c i e n c e a n d M a n a g e m e n t , K a r u r .
PROGRAMMING IN PHP (16SCCCA9,16SCCCS9) III BCA / B.Sc (CS)
2 Mark Questions
Unit – I:
Essentials of PHP - Operators and Flow Control - Strings and Arrays.
Unit – II:
Creating Functions - Reading Data in Web Pages - PHP Browser –
Handling Power.
1. What is function?
2. What refers get and post method?
3. What is the difference between get and post method?
4. What is usage of action attribute?
5. Write a note on forms.
6. What is the use of array() syntax in PHP?
7. What are global variables?
8. What is static variables?
9. What refers browser?
10. How to create the function in PHP?
11. What is mean by browser?
12. Differentiate PHP normal function and conditional function.
13. Write an usage of PHP’s server variable.
14. What is Data Validation? Give example.
15. Differentiate Client-side Data Validation and Server-side Data Validation.
16. What are hidden fields?
12 | V a l l u v a r C o l l e g e o f S c i e n c e a n d M a n a g e m e n t , K a r u r .
PROGRAMMING IN PHP (16SCCCA9,16SCCCS9) III BCA / B.Sc (CS)
Unit – III:
Object-Oriented Programming –Advanced Object-Oriented
Programming.
Unit – IV:
File Handling –Working with Databases – Sessions, Cookies, and FTP
1. Write about writing to a file in PHP.
2. Write a note on fwrite functions.
3. What is stat function? / Write a note on stat functions.
4. Write an usage of stat function in PHP.
5. Write the syntax for copy and unlink functions.
6. Write a note on flock functions.
7. What is database?
8. What is the use of join operation?
9. What refers cookie? / Define Cookies.
10. What refers FTP? / Define FTP.
11. What is a session?
12. Write a program to find a session hit counter.
13. What is meant by object serialization?
Unit – V:
Ajax – Advanced Ajax – Drawing Images on the Server.
1. What refers Ajax? / What is AJAX? / Expand the term AJAX?
2. Why we need an AJAX?
3. List out the advantages of AJAX?
4. What are the applications of AJAX?
5. Write the ready state property of XMLHTTPRequest object and its purpose.
6. What is XML?
7. Write the difference between HTML and XML.
8. What is Java Script?
9. State any four image-creating function.
10. Write a syntax on Image string functions.
11. How to display images in HTML pages? / How to insert images in HTML pages.
12. What is the function used to draw filled-in-figures
13 | V a l l u v a r C o l l e g e o f S c i e n c e a n d M a n a g e m e n t , K a r u r .
Valluvar College of Science and Management,Karur.
Department of Computer Science
April-2020
UNIT-1
5 Marks
10 Marks
UNIT-2
5 Marks
UNIT-3
5 Marks
1. What is class? Explain how to create the classes with suitable examples.
2. Explain about cloning objects.
3. Write difference between abstract class and interface.
4. Explain the concept of inheritence with suitable examples.
5. What are the role of constructor and destructor in PHP? Explain.
6. Explain the steps to creating interfaces.
7. How will you set access to properties and methods.
8. Explain about creating a static method.
9. Write short notes on “Final” keyword in PHP?
10. Explain about overloading in PHP.
11. Write short notes on FTP?
10 Marks
UNIT-4
5 Marks
10 Marks
1. How will you reading text from a file using fgets() and fgetc().
2. Explain about accessing the database in PHP.
3. Write the detail about any 5 file functions with suitable examples.
4. Explain the steps to creating and updating database in PHP.
5. Write short notes on Session and Cookies.
6. Explain downloading and uploading files in FTP.
UNIT-5
5 Marks
10 Marks
1. Explain the detail about passing data to the server with GET and POST methods.
2. How to handle concurrent AJAX requests with the XMLHTTPRequest array and with javascript
inner functions.
PROGRAMMING IN PHP (16SCCCA9/16SCCCS9) III BCA / B.SC (CS)
Important Questions
Unit – I:
2 Marks Questions
5 Marks Questions
10 Marks Questions
Unit – II:
2 Marks Questions
1. What is function?
2. What refers get and post method?
3. What is the difference between get and post method?
4. What is usage of action attribute?
5. Write a note on forms.
6. What is the use of array() syntax in PHP?
7. What are global variables?
8. What is static variables?
9. What refers browser?
10.How to create the function in PHP?
11.What is mean by browser?
12.Differentiate PHP normal function and conditional function.
13.Write an usage of PHP’s server variable.
14.What is Data Validation? Give example.
15.Differentiate Client-side Data Validation and Server-side Data Validation.
16.What are hidden fields?
5 Marks Questions
10 Marks Questions
Unit – III:
2 Marks Questions
5 Marks Questions
1. What is class? Explain how to create the classes with suitable examples.
2. Explain about cloning objects.
3. Write difference between abstract class and interface.
4. Explain the concept of inheritance with suitable examples.
5. What are the role of constructor and destructor in PHP? Explain.
6. Explain the steps to creating interfaces.
7. How will you set access to properties and methods?
8. Explain about creating a static method.
9. Write short notes on “Final” keyword in PHP?
10. Explain about overloading in PHP.
11. Write short notes on FTP?
10 Marks Questions
Unit – IV:
2 Marks Questions
5 Marks Questions
10 Marks Questions
1. How will you reading text from a file using fgets() and fgetc().
2. Explain about accessing the database in PHP.
3. Write the detail about any 5 file functions with suitable examples.
4. Explain the steps to creating and updating database in PHP.
5. Write short notes on Session and Cookies.
6. Explain downloading and uploading files in FTP.
Unit – V:
2 Marks Questions
5 Mark Questions
10 Marks Questions
Explain the detail about passing data to the server with GET and POST methods.
<?php
// This is a single line comment
# This is also a single line comment
echo 'Hello World!';
?>
4. PHP multi-line comments
<?php
/*
This is a multiple line comment block
that spans across more than
one line
*/
echo "Hello, world!";
?>
5. Creating variables in PHP
<?php
$txt = "Hello World!";
$number = 10;
// Display variables value
echo $txt;
echo "<br>";
echo $number;
?>
<?php
// Assign values to variables
$color = "blue";
$Color = "red";
$COLOR = "green";
// Try to print variables values
echo "The color of the sky is " . $color . "<br>";
echo "The color of the sun is " . $Color . "<br>";
echo "The color of the tree is " . $COLOR . "<br>";
?>
PHP Data Types
7. PHP integers
<?php
$a = 123; // decimal number
var_dump($a);
echo "<br>";
$b = -123; // a negative number
var_dump($b);
echo "<br>";
$c = 0x1A; // hexadecimal number
var_dump($c);
echo "<br>";
$d = 0123; // octal number
var_dump($d);
?>
8. PHP strings
<?php
$a = 'Hello world!';
echo $a;
echo "<br>";
$b = "Hello world!";
echo $b;
echo "<br>";
$c = 'Stay here, I\'ll be back.';
echo $c;
?>
<?php
$a = 1.234;
var_dump($a);echo "<br>";
$b = 10.2e3;
var_dump($b);echo "<br>";
$c = 4E-10; var_dump($c);
?>
<?php
// Assign the value TRUE to a variable
$show_error = True;
var_dump($show_error);
?>
<?php
$colors = array("Red", "Green", "Blue");
var_dump($colors);
echo "<br>";
$color_codes = array(
"Red" => "#ff0000",
"Green" => "#00ff00",
"Blue" => "#0000ff"
);
var_dump($color_codes);
?>
<?php
// Class definition
class greeting{
// properties
public $str = "Hello World!";
// methods
function show_greeting(){
return $this->str;
}
}
// Create object from class
$message = new greeting;
var_dump($message);
?>
<?php
$a = NULL;
var_dump($a);
echo "<br>";
$b = "Hello World!";
$b = NULL;
var_dump($b); ?>
PHP Strings
21. Differences between single and double quoted strings
<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";
echo 'Hello, $my_str!<br>';
echo '<pre>Hello\tWorld!</pre>';
echo "<pre>Hello\tWorld!</pre>";
echo 'I\'ll be back';
?>
22. Find the length of a string
<?php
$my_str = 'Welcome to Computer Science';
// Calculating and displaying string length
echo strlen($my_str);
?>
23. Replacing text within a string
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
// Display replaced string
echo str_replace("facts", "truth", $my_str);
?>
24. Reversing the direction of a string
<?php
$my_str = 'You can do anything, but not everything.';
PHP Operators
25. PHP arithmetic operators
<?php
$x = 10;
$y = 4;
echo($x + $y); echo "<br>";
echo($x - $y); echo "<br>";
echo($x * $y); echo "<br>";
echo($x / $y); echo "<br>";
echo($x % $y);
?>
26. PHP assignment operators
<?php
$x = 10;
echo $x; echo "<br>";
$x = 20;
$x += 30;
echo $x; echo "<br>";
$x = 50;
$x -= 20;
echo $x; echo "<br>";
$x = 5;
$x *= 25;
echo $x; echo "<br>";
$x = 50;
$x /= 10;
echo $x; echo "<br>";
$x = 100;
$x %= 15;
echo $x;
?>
27. PHP comparison operators
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z); echo "<br>";
var_dump($x === $z); echo "<br>";
var_dump($x != $y); echo "<br>";
var_dump($x !== $z); echo "<br>";
var_dump($x < $y); echo "<br>";
var_dump($x > $y); echo "<br>";
var_dump($x <= $y);echo "<br>";
var_dump($x >= $y);
?>
28. PHP incrementing and decrementing operators
<?php
$x = 10;
echo ++$x; echo "<br>";
echo $x; echo "<hr>";
$x = 10;
echo $x++; echo "<br>";
echo $x; echo "<hr>";
$x = 10;
echo --$x; echo "<br>";
echo $x; echo "<hr>";
$x = 10;
echo $x--; echo "<br>";
echo $x;
?>
29. PHP logical operators
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
?>
30. PHP string operators
<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Outputs: Hello World!
echo "<br>";
$x .= $y;
echo $x; // Outputs: Hello World!
?>
31. PHP array operators
<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z); echo "<hr>";
var_dump($x == $y); echo "<br>";
var_dump($x === $y);echo "<br>";
var_dump($x != $y); echo "<br>";
var_dump($x <> $y);echo "<br>";
var_dump($x !== $y); ?>
PHP Arrays
36. PHP indexed arrays
<?php
$colors = array("Red", "Green", "Blue");
// Printing array structure
print_r($colors);
?>
37. PHP associative arrays
<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);
// Printing array structure
print_r($ages);
?>
38. PHP multidimensional array
<?php
// Define nested array
$contacts = array(
array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
),
array(
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
),
array(
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
)
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>
39. Viewing array structure and values
<?php
// Define array
$cities = array("London", "Paris", "New York");
// Display the cities array
Print_r($cities);
?>
40. Get complete array information
<?php
// Define array
$cities = array("London", "Paris", "New York");
PHP Loops
49. PHP while loop
<?php
$i = 1;
while($i <= 3){
$i++;
echo "The number is " . $i . "<br>";
}
?>
50. PHP do...while loop
<?php
$i = 1;
do{
$i++;
echo "The number is " . $i . "<br>";
}
while($i <= 3);
?>
51. PHP for loop
<?php
for($i=1; $i<=3; $i++){
echo "The number is " . $i . "<br>";
}
?>
52. PHP foreach loop
<?php
$colors = array("Red", "Green", "Blue");
// Loop through count array
foreach($colors as $value){
echo $value . "<br>";
}
?>
PHP Functions
MISCELLANEOUS PROGRAMS
1. Write a PHP script to get the PHP version and configuration information.
<?php
phpinfo();
?>
6. Write a PHP script, which changes the color of the first character of a word
<?php
$text = 'PHP Tutorial';
$text=preg_replace('/(\b[a-z])/i','<span style="color:red;">\1</span>',$text);
echo $text;
?>
7. Write a PHP script, to check whether the page is called from 'https' or
'http'.
<?php
if (!empty($_SERVER['HTTPS']))
{
echo 'https is enabled';
}
else
{
echo 'http is enabled'."\n";
}
?>
8. Write a PHP script to display source code of a webpage (e.g.
"http://www.example.com/").
<?php
$all_lines = file('https://www.w3resource.com/');
foreach ($all_lines as $line_num => $line)
{
echo "Line No.-{$line_num}: " . htmlspecialchars($line) . "\n";
}
?>
9. Write a simple PHP program to check that emails are valid.
<?php
// pass valid/invalid emails
$email = "mail@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo '"' . $email . '" = Valid'."\n";
}
else
{
echo '"' . $email . '" = Invalid'."\n";
}
?>
10. Write a PHP script to get last modified information of a file.
<?php
$current_file_name = basename($_SERVER['PHP_SELF']);
$file_last_modified = filemtime($current_file_name);
echo "Last modified " . date("l, dS F, Y, h:ia", $file_last_modified)."\n";
?>
<?php
$file = basename($_SERVER['PHP_SELF']);
$no_of_lines = count(file($file));
echo "There are $no_of_lines lines in $file"."\n";
?>
<?php
echo 'Current PHP version : ' . phpversion();
// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy')."\n";
?>
13. Write a PHP script to delay the program execution for the given number of
seconds.
<?php
// current time
echo date('h:i:s') . "\n";
// sleep for 5 seconds
sleep(5);
// wake up
echo date('h:i:s')."\n";
?>
<?php
echo $x;
print_r(error_get_last());
?>
<?php
$full_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $full_url."\n";
?>
16. Write a PHP script to get the names of the functions of a module.
<?php
print_r(get_extension_funcs("JSON"));
echo "\n";
print_r(get_extension_funcs("XML"))."\n"; ?>
17. Write a PHP script to get the document root directory under which the current
script is executing, as defined in the server's configuration file.
<?php
// getenv() gets the value of an environment variable
$rd = getenv('DOCUMENT_ROOT');
echo $rd."\n";
?>
18. Write a PHP script to get the information about the operating system PHP is
running on.
<?php
echo php_uname()."\n";
echo PHP_OS."\n";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!'."\n";
}
?>
Note:
While writing the programs first you have to write the logic for the program then you
have to write the code. For example,
Factorial Program:
In mathematics, the factorial of a positive integer n, denoted by n!, is the product of
all positive integers less than or equal to n:
n! = n * (n-1) * (n-2) * (n-3)*...*3*2*1.
For example,
5! = 5 * 4 * 3 * 2 * 1 = 120.
Likewise you have to write the logic for each program and then you have to convert
the logic into code.
Refer your PHP lab manual for assistance.