Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Search:

CA216      CA249      CA318

CA400      CA651      CA668


PHP

Server-side scripts.



file.php

Contains normal HTML, and then PHP code mixed in, delimited as follows:

<html>
<body>

html code

<?php 
php code 
?>

more html code

PHP gets interpreted on server side before resulting HTML stream is returned to client.
Client can't see PHP code: Unlike the case with Javascript, client can't see the code that generated the HTML.
PHP used for interaction with data on server (e.g. database).



Reference






Debugging




Environment



Accessing command-line



Example



Make HTTP requests to other sites from server



Call with arguments

PHP can be used with arguments in the URL.
This is like a CGI script with HTTP GET.
Like a Google search: search?q=biscuits

Usage like:

http://host/prog.php?x=value
PHP can parse the URL using parse_str

 
<?php
	
$qs=$_SERVER["QUERY_STRING"];

parse_str ( $qs );

// $x is now defined as whatever the value was in the URL section: x=value 
	 
 print " x is $x "; 
 
?>

This overwrites any existing variables so is not very safe.
Safer method uses an array to fetch the values:

  
<?php
	
$qs=$_SERVER["QUERY_STRING"];

parse_str ( $qs, $a );
	 
$x = $a["x"];

 print " x is $x "; 

?>
 
Can use PHP to parse the URL early so can use the variables in the head section of the page (e.g. in the title):
 
<html>

<?php
	
$qs=$_SERVER["QUERY_STRING"];

parse_str ( $qs, $a );
	 
$x = $a["x"];

?>

<head>

<?php
	 
 print "<title> Page for x = $x </title> "; 

?>
 
</head>






Stand-alone PHP scripts

Can use PHP as just general offline scripting language.
Can run Stand-alone PHP scripts at command-line. Something like:

#!/usr/bin/php 

Whatever is outside PHP is just displayed.

<?php

// echo PHP_VERSION;

 print "Name of script = $argv[0] \n"; 
 print "First command-line arg = $argv[1] \n"; 

 print "All command-line args: \n";
 print_r($argv); 
 print "\n"; 

?>

Whatever is outside PHP is just displayed.


Feeds      w2mind.org

On Internet since 1987.