Classic loop example
<HTML>
<HEAD>
<TITLE>Classic loop example</TITLE>
</HEAD>
<BODY>
<?
for($counter = 1; $counter <= 6; $counter++) //Loop 6 times
{
print("<B>counter is $counter</B><BR>n"); //Print 6 times
}
?>
</BODY>
</HTML>
Advanced uses of for
<HTML>
<HEAD>
<TITLE>Advanced uses of for</TITLE>
</HEAD>
<BODY>
<?
/*
**Print necessary description text
*/
print("<B>How many days until Monday?</B>n");
print("<OL>n");
for($currentDate = date("U"); //Define $currentDate time format
date("l", $currentDate) != "Monday"; //Determine whether the current system time is Monday
$currentDate += (60 * 60 * 24)) //Current time plus 1 day
{
/*
**Print time name
*/
print("<LI>" . date("l", $currentDate) . "n");
}
print("</OL>n");
?>
</BODY>
</HTML>
Simple call of function:
<HTML>
<HEAD>
<TITLE>Simple function</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printBold($inputText) //Define function printBold()
{
print("<B>" . $inputText . "</B>"); ////Print $inputText
}
print("This line is not emphasized!<BR>n"); //Print the string directly
printBold("This line is aggravated!!!"); //Call function printBold() function
print("<BR>n");
print("This line is not emphasized!<BR>n"); //Print the string directly
?>
</FONT>
</BODY>
</HTML>
functions that return values
<HTML>
<HEAD>
<TITLE>Function with return value</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function makeBold($inputText) //Define function makeBold() function
{
$boldedText = "<B>";
$boldedText .= $inputText;
$boldedText .= "</B>";
return($boldedText); //return variable $boldedText
}
print("This line is not emphasized!!!<BR>n"); //Print the string directly
print(makeBold("This line is emphasized!!!") . "<BR>n");//Call function makeBold() function
print("This line is not emphasized!!!<BR>n"); //Print the string directly
?>
</SIZE>
</BODY>
</HTML>
Function with default parameters
<HTML>
<HEAD>
<TITLE>Functions with default parameters</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printColored($Text, $Color="black") //Define function function
{
print("<FONT COLOR="$Color">$Text</FONT>"); //Get the content and color of the string
}
printColored("This is a black word!"); //Call function
print("<BR><BR>n");
printColored("This is a blue word!", "blue"); //Call function
print("<BR>n");
?>
</SIZE>
</BODY>
</HTML>
The algorithm used to determine whether it is an integer
<HTML>
<HEAD>
<TITLE>Judge integers</TITLE>
</HEAD>
<BODY>
<?
function checkInteger($Number)
{
if($Number > 1)
{
/* An integer minus 1 is still an integer*/
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* For a negative number, */
/* Its absolute value can be analyzed*/
return(checkInteger((-1)*$Number-1));//Take the absolute value and analyze negative numbers as integers
}
else
{
if(($Number > 0) AND ($Number < 1))
{
return("Of course not");
}
else
{
/* 0 and 1 are integers */
/* According to relevant mathematical definitions*/
return("Yes");
}
}
}
print("<B>Is 0 an integer?</B>" .
checkInteger(0) . "<BR>n");
print("<B>Is 7 an integer?</B> " .
checkInteger(7) . "<BR>n");
print("<B>What about 3.5?</B>" . checkInteger(3.5) . "<BR>n");
print("<B>What about -5?</B>" . checkInteger(-5) . "<BR>n");
print("<B>There is -9.2?</B>" . checkInteger(-9.2) . "<BR>n");
?>
</BODY>
</HTML>
Initialize array
<HTML>
<HEAD>
<TITLE>Initializing array</TITLE>
</HEAD>
<FONT SIZE=5>
<?
$monthName = array(1=>"January", "February", "March",//Initialize an array
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(""May" in English is<B> $monthName[5] </B>.<BR>n");//Print the 6th element in the array
?>
</FONT>
</BODY>
</HTML>
Get elements in array
<HTML>
<HEAD>
<TITLE>Get the elements in the array</TITLE>
</HEAD>
<?
$monthName = array(
/*Define $monthName[1] to $monthName[12]*/
1=>"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/*Define $monthName["Jan"] to $monthName["Dec"]*/
"Jan"=>"January", "Feb"=>"February",
"Mar"=>"March", "Apr"=>"April",
"May"=>"May", "Jun"=>"June",
"Jul"=>"July", "Aug"=>"August",
"Sep"=>"September", "Oct"=>"October",
"Nov"=>"November", "Dec"=>"December",
/*Define $monthName["Jan"] to $monthName["Dec"]*/
"January"=>"January", "February"=>"February",
"March"=>"March", "April"=>"April",
"May"=>"May", "June"=>"June",
"July"=>"July", "August"=>"August",
"September"=>"September", "October"=>"October",
"November"=>"November", "December"=>"December"
);
/*Print related elements*/
print("Month <B>5</B> is <B>" . $monthName[5]. "</B><BR>n");
print("Month <B>Aug</B> is <B>" . $monthName["Aug"] . "</B><BR>n");
print("Month <B>June</B> is <B>" . $monthName["June"] . "</B><BR>n");
?>
</BODY>
</HTML>
Create a multidimensional array
<HTML>
<HEAD>
<TITLE>Create a multidimensional array</TITLE>
</HEAD>
<?
$Cities = array( //two-dimensional array array()
"North China"=>array(
"Beijing City",
"Tianjin City",
"Shijiazhuang"
),
"Northwest Region"=>array(
"Xi'an",
"Lhasa"
)
);
print("North China: ".$Cities["North China"][0]); //Print $Cities["North China"][0]
?>
</BODY>
</HTML>
PHP 4.0 implements table-like printing
<HTML>
<HEAD>
<TITLE>Realize table-like printing</TITLE>
</HEAD>
<BODY>
<?
/*
** Data tabulation
*/
print("<TABLE bgcolor='ffccoo' BORDER="1">n"); // Start of table
for($Row=1; $Row <= 12; $Row ++)
{
print("<TR>n"); // start line
// do each column
for($Column=1; $Column <= 12; $Column ++)
{
print("<TD>");//Start column
print($Row * $Column);//Multiple table elements
print("</TD>");
}
print("</TR>n"); // end of line
}
print("</TABLE>n"); // End of table
?>
</BODY>
</HTML>
View some variables of the system
<HTML>
<HEAD>
<TITLE>View PHP environment variables</TITLE>
</HEAD>
<BODY>
<?
print("The name of the file you are using is: ");
print(__FILE__);
print(" <BR>n");
print("<hr>");
print("Your operating system is: ");
print(PHP_OS);
print("<hr>");
print("Your php version is: ");
print(PHP_VERSION)
?>
</BODY>
</HTML>
Open local or remote file
<HTML>
<HEAD>
<TITLE>Open local or remote file</TITLE>
</HEAD>
<BODY>
<?
print("<H3>Open file via http protocol</H3>n");
//Open the file via http protocol
if(!($myFile = fopen("d:web/web/php/test/data.txt", "r")))
{
print("File cannot be opened");
exit;
}
while(!feof($myFile)) //Loop
{
// Read the contents of the file line by line
$myLine = fgetss($myFile, 255);
print("$myLine <BR>n");
}
//Close the file handle
fclose($myFile);
?>
</BODY>
</HTML>
Comparison of several ways to open files
<HTML>
<HEAD>
<TITLE>Read file content</TITLE>
</HEAD>
<BODY>
<?
//Open the file and print every character of the file
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>
<?print("<hr>");?>
<?
//Open the file and print each line of the file
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgets($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
<?print("<hr>");?>
<?
/* Open the file and print each line of the file,
At the same time, remove the HTML language in the retrieved string.
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgetss($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
</BODY>
</HTML>
Access common file attributes
<HTML>
<HEAD>
<TITLE>Access common file attributes</TITLE>
</HEAD>
<BODY>
<BR>
<?
print("Owner of file (UID value):");
print(fileowner("data.txt")."<br>");
print("File size:");
print(filesize("data.txt")."<br>");
print("File type:");
print(filetype("data.txt")."<br>");
?>
</BODY>
</HTML>
Recall text file content
<HTML>
<HEAD>
<TITLE>Calling text file content</TITLE>
</HEAD>
<BODY>
<CENTER>
<?
// Open the file and print each line
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."<BR>");
}
?>
</CENTER>
</BODY>
</HTML>
Create directory function
<HTML>
<HEAD>
<TITLE>Create directory function</TITLE>
</HEAD>
<BODY>
<?
if(mkdir("myDir1", 0777)) //Function to create a directory
{
print("Directory created successfully"); //Directory created successfully
}
else
{
print("Directory creation failed!"); //Directory creation failed!
}
?>
</BODY>
</HTML>
Browse catalog
<HTML>
<HEAD>
<TITLE>Browse directory</TITLE>
</HEAD>
<BODY>
<?
// Browse the structure of the directory using tables
print("<TABLE BORDER="1">n");
//Create the header of the table
print("<TR><font color='red'>n");
print("<TH>File name</TH>n");
print("<TH>File size</TH>n");
print("</font></TR>n");
$myDirectory = opendir("."); // Create a handle to the operating directory
// Read each sub-item in the directory
while($entryName = readdir($myDirectory))
{
print("<TR>");
print("<TD>$entryName</TD>");
print("<TD ALIGN="right">");
print(filesize($entryName));
print("</TD>");
print("</TR>n");
}
closedir($myDirectory); // Close the directory
print("</TABLE>n");
?>
</BODY>
</HTML>
PHP related information
<HTML>
<HEAD>
<TITLE>PHP related information</TITLE>
</HEAD>
<BODY>
<?
phpinfo();
?>
</BODY>
</HTML>
Commonly used numerical judgment functions
<HTML>
<HEAD>
<TITLE>Commonly used numerical judgment functions</TITLE>
</HEAD>
<BODY>
<?
//Judge array
$colors = array("red", "blue", "green");
if(is_array($colors))
{
print("colors is an array"."<br>");
}
//Double precision number judgment
$Temperature = 15.23;
if(is_double($Temperature))
{
print("Temperature is a double"."<br>");
}
//Integer judgment
$PageCount = 2234;
if(is_integer($PageCount))
{
print("$PageCount is an integer"."<br>");
}
//Object judgment
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing is an object"."<br>");
}
//Character judgment
$Greeting = "Hello";
if(is_string($Greeting))
{
print("Greeting is a string"."<br>");
}
?>
</BODY>
</HTML>
File upload interface
<HTML>
<HEAD>
<TITLE>File upload interface</TITLE>
</HEAD>
<BODY><TABLE><CENTER>
<?
if($UploadAction){
$UploadAction=0;
$TimeLimit=60;
/*Set the timeout limit time. The default time is 30s. When set to 0, it is unlimited*/
set_time_limit($TimeLimit);
If(($Upfile != "none")&&
($Upfile != ""))
{
$Filepath="d:webwebphptest"; //Upload file storage path
$FileName=$Filepath.$Upfile_name;
if($Upfile_size <1024) //Upload file size
{$FileSize = (string)$Upfile_size . "bytes";}
elseif($Upfile_size <(1024 * 1024))
{
$FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB";
}
if(!file_exists($FileName))
{
if(copy($Upfile,$FileName))
{unlink($Upfile);
echo "<br><br>n";
echo "File $Upfile_name has been uploaded successfully!";
echo "<br><br>n";
echo "File location: $FileName";
echo "<br><br>n";
echo "File size: $FileSize";
echo "<br><br>n";
}
else
{echo "File $Upfile_name upload failed!"; }
}
else
{echo "The file $Upfile_name already exists!"; }
}
else
{echo "You did not select any files to upload!"; }
set_time_limit(30); //Restore default timeout settings
}
?>
<FORM ENCTYPE = "multipart/form-data" NAME = "SubmitForm"
ACTION = "default.php" METHOD = "POST">
<INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE = "1000000">
<INPUT TYPE = "hidden" NAME = "UploadAction" VALUE = "1">
<TR><TD><INPUT NAME = "Upfile" TYPE = "file" SIZE = "30"></TD>
</TR><TR><TD><INPUT NAME = "submit" VALUE = "submit" TYPE = "submit">
<INPUT NAME = "reset" VALUE = "Reset" TYPE = "reset"></TD>
</TR></FORM></CENTER></TABLE>
</BODY>
</HTML>