PHP Material
PHP Material
xhr.open("POST", "AddNos.jsp");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("tno1=100&tno2=200");
<?php <?php
// PHP code goes here echo "<h1>Hello from PHP</h1>";
?> ?>
<!DOCTYPE html> A PHP file normally contains HTML tags, and somePHP
<html> scripting code.
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
// a single-line comment
• Comments in PHP : # also a single-line comment
/*
▪ PHP supports several ways a multiple-lines comment block
spanning multiple lines
*/
$x = "15" + 27;
echo($x); //gives 42!!
Example, $popeye, $one and $INCOME are all valid PHP variable names,
while $123 and $48hrs are invalid.
<?php
$x = 5; // global scope
function myTest() {
echo "<p>Variable x inside function is: $x</p>"; // x inside function gives error
$y = 10; // local scope
echo "<p>Variable y inside function is: $y</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>"; // y outside function gives error
?>
<?php
function myTest() { Note: The variable is still local to the
static $x = 0; function.
echo $x; $x++;
} Outputs : 0 1 2
myTest();
myTest();
myTest(); ?>
April 10, 2019 - 16 -
Outputdata to the
screen
• There are two basic ways to get output: echo and print
• print_r() and var_dump() : dumps out PHP data - it is used mostly for
debugging
$stuff = array("name" => "Chuck", "course" => "SI664");
print_r($stuff);
• Constants are automatically global & can be used across the entire script
• Default Parameters
function getPreferences($whichPreference = 'all') {
//code that uses the parameter
}
• Variable Parameters: PHP provides three functions you can use in the
function to retrieve the parameters passed to it.
▪ func_get_args() returns an array of all parameters provided to the function;
▪ func_num_args() returns the number of parameters provided to the function;
▪ func_get_arg() returns a specific argument from the parameters.
$array = func_get_args();
$count = func_num_args();
$value = func_get_arg(argument_number);
April 10, 2019 - 24 -
PHP
include
• INCLUDE appends the code from an external file into the current file.
▪ Including files is very useful when you want to include the same PHP, HTML, or text on
multiple pages of a website
▪ Syntax : INCLUDE ("external_file_name");
$date=date_create("2013-03-15");
print_r($date);
//DateTime Object ( [date] => 2013-03-15 00:00:00.000000 [timezone_type] => 3 [timezone]=>
Europe/Berlin )
echo date_format($date,"Y/m/d H:i:s"); //2013/03/15 00:00:00
• $_GET: used to collect form data after submitting an HTML form with
method="get“
<body>
<a href="test_get.php?fname=Anil&lname=Patil">Test $GET</a>
</body>
Mainpage.php
<body>
<?php echo "Study " . $_GET[‘fname'] . " at " . $_GET[‘lname']; ?>
</body>
test_get.php
▪ Now, when the user submits the form, $_GET['languages'] contains an array
instead of a simple string.
▪ This array contains the values that were selected by the user.
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
▪ session_unset() : to remove session data, simply unset the corresponding key of the
$_SESSION
▪ session_destroy() : destroy the session
SessionExample2.php
$to = “[email protected]";
$subject = "Hi There!";
$body = "PHP is one of the best scripting languages around";
$headers = "From: [email protected]\n";
mail($to,$subject,$body,$headers);
cookieSetExample.php
cookieGetExample.php
Append. Opens the file for writing only. Preserves file content by writing to the end of the file. If
a
files does not exist then it attempts to create a file.
Read/Append. Opens the file for reading and writing. Preserves file content by writing to the end
a+
of the file. If files does not exist then it attempts to create a file.
April 10, 2019 - 50 -
File handling
(read)
▪ filesize(filename) : returns file length in bytes
▪ fread(filepointer, length of file) : read a file that is opened with fopen()
▪ fclose(filepointer) : close the file
<?php
$filename = "sample.txt";
$fileptr = fopen( $filename, "r" );
if( $fileptr == false ) {
echo ( "Error in opening file" ); exit();
}
$filesize = filesize( $filename );
$filetext = fread( $fileptr, $filesize );
fclose( $fileptr );
echo ( "File size : $filesize bytes" );
echo ( "<pre>$filetext</pre>" );
?>
• file() - Reads entire file into an array with each line of the file corresponding
to an element of the array
$filename = "sample.txt";
$dataArr = file($filename);
print_r($dataArr);
emp.csv
Output – reads a
single line
readCSV.php
<?php
if(!file_exists("welcome.txt")) {
die("File not found");
} else {
$file=fopen("welcome.txt","r");
}
?>
db4-getConn.php
db5-InsertRow.php
<body>
<div class="container">
<h1>Container</h1>
<p>container content</p>
</div>
</body>
▪ To add a subtitle beside the title of the page, you can put it inside the same <h1> tag that
we used before; wrap the subtitle inside a <small></small> tag
▪ Panels come with various color options • panel-primary for dark blue
• panel-success for green
• panel-info for sky blue
• panel-warning for yellow
• panel-danger for red
April 10, 2019 - 75 -
Page Components :
Thumbnails
• You can add some excerpts to each thumbnail and a Read More button for
linking to different pages in your website.
▪ Use <div> instead of <a>.
▪ Then add an excerpt using <p> inside the “caption” div and a link with a “Read More”
anchor and classes btn and btn-primary inside the same “caption” div.
▪ We can also apply various colors to each list item by adding list-group-item-* classes along
with list-group-item.
▪ Next, we'll use a div with class container-fluid inside this navbar element.
▪ class label-default is necessary to tell Bootstrap which variant of label we want to use.
The available label variants are:
o label-default for gray
o label-primary for dark blue
o label-success for green
o label-info for light blue
o label-warning for orange
o label-danger for red
<div class="jumbotron">
<h1>Bootstrap Tutorial</h1>
<p>Bootstrap is the most popular HTML, CSS, and JS framework for
developing responsive, mobile-first projects on the web.</p>
</div>