SoFunction
Updated on 2025-04-13

A few classic codes that must be collected in PHP


PHP 4.0 realizes table printing

<HTML>
<HEAD>
<TITLE>Implement table printing</TITLE>
</HEAD>
<BODY>
<?
    /* 
** Data tabularization
    */

print("<TABLE bgcolor='ffccoo' BORDER=\"1\">\n"); // The table starts


    for($Row=1; $Row <= 12; $Row ++)
    {
print("<TR>\n");// Start

        // do each column
        for($Column=1; $Column <= 12; $Column ++)
        {
print("<TD>");//Start column
print($Row * $Column);//Table elements multiply
            print("</TD>");
        }

print("</TR>\n"); // The end of the line

    }

print("</TABLE>\n");// End of the table

?>
</BODY>
</HTML>

View some variables in the system

<HTML>
<HEAD>
<TITLE>View PHP environment variables</TITLE>
</HEAD>
<BODY>
<?
print("You are using the name of the file:");
    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 files

<HTML>
<HEAD>
<TITLE>Open local or remote files</TITLE>
</HEAD>
<BODY>
<?
print("<H3>Open file through http protocol</H3>\n");
// Open the file through the http protocol
    if(!($myFile = fopen("d:web/web/php/test/", "r")))
    {
print("File cannot be opened");
        exit;
    }
while(!feof($myFile))                                                         �
    {
// Read content in the file by line
        $myLine = fgetss($myFile, 255);
        print("$myLine <BR>\n");
    }
// Close the file handle
    fclose($myFile);
?>
</BODY>
</HTML>
Comparison of several ways to open a file
<HTML>
<HEAD>
<TITLE>Read file content</TITLE>
</HEAD>
<BODY>
<?
// Open the file and print every character of the file at the same time
    if($myFile = fopen("", "r"))
    {
    while(!feof($myFile))
    {
        $myCharacter = fgetc($myFile);
        print($myCharacter);
    }
    fclose($myFile);
    }    
?>
<? print("<hr>");?>
<?
// Open the file and print every line of the file
    if($myFile = fopen("", "r"))
    {
        while(!feof($myFile))
        {
            $myLine = fgets($myFile, 255);
            print($myLine);
        }
    fclose($myFile);
    }    
?>
<? print("<hr>");?>
<?
/* Open the file and print every line of the file at the same time,
At the same time, remove the HTML language in the string.
    */
    if($myFile = fopen("", "r"))
    {
        while(!feof($myFile))
        {
            $myLine = fgetss($myFile, 255);
            print($myLine);
        }
        fclose($myFile);
    }    
?>
</BODY>
</HTML>
Accessing common properties of files

<HTML>
<HEAD>
<TITLE>Accessing common properties of files</TITLE>
</HEAD>
<BODY>
<BR>
<?
print("Owner of the file (UID value):");
    print(fileowner("")."<br>");
print("File size:");
    print(filesize("")."<br>");
print("File type:");
    print(filetype("")."<br>");
?>
</BODY>
</HTML>
Call text file content
<HTML>
<HEAD>
<TITLE>Call text file content</TITLE>
</HEAD>
<BODY>
<CENTER>
<?
// While opening the file, print each line
    $myFile = file( "");
    for($index = 0; $index < count($myFile); $index++)
    {
        print($myFile[$index]."<BR>");
    }
?>
</CENTER>
</BODY>
</HTML>
Create directory functions
<HTML>
<HEAD>
<TITLE>Create directory function</TITLE>
</HEAD>
<BODY>
<?
if(mkdir("myDir1", 0777))                                                     �
    {
print("Directory creation successfully");           //Directory creation successfully
    }
    else
    {
print("Directory establishment failed!");          //Directory establishment failed
    }
?>
</BODY>
</HTML>
Browse the directory
<HTML>
<HEAD>
<TITLE>Browse the directory</TITLE>
</HEAD>
<BODY>
<?
// Use tables to browse the structure of the directory
    print("<TABLE BORDER=\"1\">\n");
// Create the header of the table
    print("<TR><font color='red'>\n");
print("<TH>filename</TH>\n");
print("<TH>file size</TH>\n");
    print("</font></TR>\n");
$myDirectory = opendir(".");                                                        �
// Read out every child 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>Common numerical judgment function</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 default time to 30s, and when set to 0, it is unlimited */
set_time_limit($TimeLimit); 
If(($Upfile != "none")&& 
($Upfile != "")) 

$Filepath="d:\web\web\php\test";                                                     �
$FileName=$Filepath.$Upfile_name; 
if($Upfile_size <1024)
{$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 "File $Upfile_name already exists!"; }

else 
{echo "You did not select any file to upload!"; }
set_time_limit(30);                                                          �
}
?> 
<FORM ENCTYPE = "multipart/form-data" NAME = "SubmitForm" 
ACTION = "" 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>