GENERAL

WinSock in VB
WinSock multiserver
Java (Pocket PC)
DHTML
CSS
Flash/XML
ASP
PHP
JavaScript
Java
Delphi
RSA
WAP
Java Sockets
Subnetting
RS-232
Zip/UnZip (Java)
Word

Source Code
Pascal
Java
C Programming
Applied PC Interfacing
WinSock client
WinSock server
FTP client
HTTP client
Java client/server
HTTP client
Subnetting in VB
RS-232 in VB
WinSock Multiserver
 Design | SFC | Adv. Sec. | Code | Diary | WWW | Essay | Research

PHP - Simple examples

The following are a few simple examples of PHP, but highlight its power, and usage.

Printing to the output

The first example simply uses the PHP print statement to display text to the output screen.

Hello World PHP code

<?php
print("Hello World");
?>

Executed code

Hello World

Strings can support escape sequence characters, such as:

Escape sequence

Description

\n

Start a new line

\r

Carriage return

\t

Tab space

\\

Backslash

\"

Double quote

\$

Dollar sign

\x0yy

Hexadecimal character (yy)

For example:

Escape characters in PHP code

<?php
print("The cost of the subscription is \$10");
print("<br>Please log on with the user name of <i>\"fred\"</i> with a password of <i>\"bert\"</i>");
print("<br>Your home directory is <b>C:\\docs\\fred</b>");
?>

Executed code

The cost of the subscription is $10
Please log on with the user name of "fred" with a password of "bert"
Your home directory is C:\docs\fred

Comments

PHP is very much based on C and UNIX-type syntax, thus comments of the form /* and */, and // can be used within the PHP code, and are completely ignored by the PHP processor. The # character can also be used in a similar way to the // sequence.

Comments in PHP code

<?php
echo "Testing ...."; // This shows the C++ type of comment
/* And this is the C type of comment
which is useful for multiple lines */

echo "... 123 ... ";
echo "... 456 ..."; # And a shell style comment
?>

Executed code

Testing ....... 123 ... ... 456 ...

Integrating with HTML

As previously seen, PHP supports the integration of HTML tags with the PHP statements. For example the following code shows an example of creating a hypertext link in PHP. If an string is to be defined with the PHP string, then the inverted commas (") and be replaced with a single quote (').

Integrating PHP code with HTML

<?php
print("<a href='php_test.php'>PHP Test</a>");
print("<br>This is an <b>important</b> example");
?>

Executed code

PHP Test
This is an important example

Variables

Variables have a proceeding '$' sign, and are followed by a space or an end of string. If you cannot get a space after a variable then the variable can be enclosed in curly brackets ({ and }). This is useful when there are no spaces between one variable and the next.

Examples of variables in PHP

<?php
$value=100;
$username="eee";
$code=123;
print("<BR>Value is $value");
print("<BR>Your user name will be ${username}$code");
?>

Executed code


Value is 100
Your user name will be eee123

Data types

The main data types are Boolean, integer, float, string, array and object. PHP differs from many other languages in that there is no integer division, thus 1 divided by 2 gives 0.5. PHP is not a strongly typed language, thus a numeric variable is automatically created when a variable is first assigned (using the = character) to a numerical value, after which it can be used as an integer or a floating-point value. Numeric values are assigned to be in decimal, unless they have 0x (for hexadecimal) or 0 (for octal) in front of them.

Examples of data types in PHP

<?php
$a = 24; # decimal number
$b = -24; # a negative number
$c = 024; # octal number
$d = 0x24; # hexadecimal number
print "<P>$a, $b, $c, $d";
$val1=1;
$val2=2;
$result=1/2;
print "<P>Result of 1/2 is $result";
?>

Executed code

24, -24, 20, 36

Result of 1/2 is 0.5

Strings

Strings can be defined in quotes (between ' and ') or double quotes (between " and "). If you want a quote in a string use \", for a new line use \n and a backslash is \\. Also stings are concatenated together using the '.' operator.

Examples of data types in PHP

<?php
$name1 = "Fred ";
$name2 = "Smith";
print "Hello \"Fred\". How are you?";
print "<br>Your full name is " . $name1 . " " . $name2;
$name1 .= $name2;
print "<br>Your full name is " . $name1
?>

Executed code

Hello "Fred". How are you?
Your full name is Fred Smith
Your full name is Fred Smith

 

 

 

 
 Design | SFC | Adv. Sec. | Code | Diary | WWW | Essay | Research