PHP

cv -|- home -|- PHP Index

What can you do if your site's host system supports dynamically generating html in response to an HTTP GET? Some might say, just about anything that the server alone can do, but there are some security barriers ;-))

In the browser you type an address, Universal Resource Locator, URL, and click Go, a HTTP GET packet is dispatched ... When it arrives at the Server, if Apache and php are loaded, any php code will be run, and the resultant html will be dispatched, for display by the browser.

The server side php can be mixed with client side scripts, like say JavaScript, so it adds to the arsenal of tools to enhance the user experience. It is an 'Open Source' alternative to say MS Active Server Page, .asp, technologies.

Php is even more useful when combined with a SQL database, like say MySQL, an 'Open Source' example. With relatively simple code, you can register a new user, allow login, logout, accessing say SQL job lists, varying the list according to switches, conditions. 

It is an Object Oriented based language, but full OO is certainly not claimed by the main development group. Read more at the php site . The manual is a pool of sample source. Try typing 'filemtime', a php function, search and scroll down. Lots of samples of how to use this function that returns a Date() value. Just browsing on to the next, next, function can offer quick insights into the strengths of php.

As a programmer I have watched its development and deployment for many years, but not until version 4 was in modest distribution that I got my fingers working on this intriguing code. Some of my test samples are left for posterity ;-)) I had been fascinated for a long time with what I suppose are called URL variables. Take for example a URL like -
   'http://geoffmclane.com/url_var.php?type=show&sort=desc&foo=bar'
What is the '?type=show&sort=desc&foo=bar' string, appended to the URL?

 These variable, and their values are available in a global array, and can be read by a simple $_GET('type'), and thus the display, the HTML generated, and sent to the client via HTTP can be varied by the simple code -
if( $_GET('type')=='show') echo 'show is on'; Try sample.

In essence variables are passed to yourself, to control what is shown, but this is only one of the strengths of php. Try here to continue with the online php.net manual.

cv -|- home -|- index -|- top

Writing php code is relatively easy if you have a C/C++/C# background. And you can download tools that will not only help write php, but give inline, or at least online, help. There are a few ways to start php code, but the preferred way is begin with <?php and end with ?>, as it allows the use of PHP in XML-conformant code such as XHTML.

As stated, read more at the php site .

Here is some sample code, interfacing with MySQL -

<?php
// testdb3.php - CGI test of creating, modifying, adding, querying, removing a table ...
printf('<body background="cldsg.jpg">');printf("\n");
printf('<h1 align="center">MySQL Test</h1>');
printf("Attempting to connect via 1.2.3.4, USERname, pwd5 ...<br>");
$db = mysql_connect("1.2.3.4.", "USERname", "pwd5");
if (!db) { die("Couldn't connect to MySQL"); }
mysql_select_db("DBname",$db) or die("Select Database Error: ".mysql_error());
printf("Connected via 1.2.3.4 to DBname}... check for 'company' table ...\n<br>");
mysql_query("Drop TABLE IF EXISTS company") or die("Drop table Error: ".mysql_error());
printf("Creating a table called 'company', with id, name ...\n<BR>");
mysql_query("CREATE TABLE company( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), companyname
VARCHAR(50) )") or die("Create table Error: ".mysql_error());
printf("Altering the table, 'company', adding 'email' ...\n<BR>");
mysql_query("ALTER TABLE company ADD COLUMN addremail VARCHAR(50) AFTER companyname") or
die("Alter table Error:".mysql_error());
printf("Inserting 2 entries into the table, 'company' ...\n<BR>");
mysql_query("INSERT INTO company (companyname, addremail ) values ('Exetel Pty Ltd',
'admin@exemail.com.au' )") or die("Insert table Error:".mysql_error());
mysql_query("INSERT INTO company (companyname, addremail ) values ('Next Pty Ltd',
'admin@next.com.au' )") or die("Insert table Error:".mysql_error());
printf("Doing a query on the table, 'company' ...\n<BR>");
$result = mysql_query("SELECT id, companyname, addremail FROM company",$db);
$max=mysql_num_rows($result);
if( $max ) {
printf('<TABLE border="1">');printf("<TR>\n");printf("<TD>\n");
printf("ID");
printf("</TD>\n");printf("<TD>\n");
printf("Name");
printf("</TD>\n");printf("<TD>\n");
printf("Email");
printf("</TD>\n");printf("</TR>\n");
for( $i = 0; $i < $max; $i++ ) {
printf("<TR>\n");printf("<TD>\n");
printf(mysql_result($result,$i,"id"));
printf("</TD>\n");printf("<TD>\n");
printf(mysql_result($result,$i,"companyname"));
printf("</TD>\n");printf("<TD>\n");
printf(mysql_result($result,$i,"addremail"));
printf("</TD>\n");printf("</TR>\n");
}
printf("</TABLE>\n");
}
printf("<b>Output $max entries ...</b>\n");
printf("</body>\n");
// eof - testdb3.php

When run, this php script will produce the following HTML page view - [
                       MySQL Test
Attempting to connect via 1.2.3.4, USERname, pwd5 ...
Connected via 1.2.3.4 to DBname... check for 'company' table ...
Creating a table called 'company', with id, name ...
Altering the table, 'company', adding 'email' ...
Inserting 2 entries into the table, 'company' ...
Doing a query on the table, 'company' ...
ID Name Email
1 Exetel Pty Ltd admin@exemail.com.au
2 Next Pty Ltd admin@next.com.au
Output 2 entries ...
]

So, the HTTP server, has run a CGI script, php in this case, and produced a HTML page to be display in any browser connected to the web ... the source returned by the HTTPd / CGI server would be [
<body background="cldsg.jpg">
<h1 align="center">MySQL Test</h1>
Attempting to connect via 1.2.3.4, USERname, pwd5 ...<br>
Connected via 1.2.3.4 to DBname... check for 'company' table ...
<br>Creating a table called 'company', with id, name ...
<BR>Altering the table, 'company', adding 'email' ...
<BR>Inserting 2 entries into the table, 'company' ...
<BR>Doing a query on the table, 'company' ...
<BR><TABLE BORDER="1"><TR>
<TD>ID</TD>
<TD>Name</TD>
<TD>Email</TD>
</TR>
<TR><TD>1</TD>
<TD>Exetel Pty Ltd</TD>
<TD>
admin@exemail.com.au</TD>
</TR>
<TR><TD>2</TD><TD>
Next Pty Ltd</TD>
<TD>
admin@next.com.au</TD>
</TR>
</TABLE>
<b>Output 2 entries ...</b>
</body>
]

The above is the 'source' view ... the data that was transferred over the web ...

cv -|- home -|- index -|- top

Reference: http://www.php.net/ - main PHP site ...

top

checked by tidy  Valid HTML 4.01 Transitional