Several functions required for getting started with PHP _ PHP Tutorial

Source: Internet
Author: User
Several code functions required for getting started with PHP. Classic loop example HTMLHEADTITLE classic loop example TITLEHEADBODY? For ($ counter1; $ counter6; $ counter ++) loop 6 Times {print (Bcounteris $ counterBBRn); print 6 times }? BODYH Classic loop example


Classic loop example


For ($ counter = 1; $ counter <= 6; $ counter ++) // 6 cycles
{
Print (" Counter is $ counter
N "); // Print 6 times
}
?>



Advanced application of


Advanced application of


/*
** Print necessary instructions
*/
Print (" How many days are there from Monday?N ");
Print ("
    N ");
    For ($ currentDate = date ("U"); // defines the $ currentDate time format
    Date ("l", $ currentDate )! = "Monday"; // determines whether the current system time is Monday.
    $ CurrentDate + = (60*60*24) // add 1 day to the current time
    {
    /*
    ** Print the time name
    */
    Print ("
  1. ". Date (" l ", $ currentDate)." n ");
    }
    Print ("
N ");
?>



Simple function call:


Simple functions



Function printBold ($ inputText) // defines the function printBold ()
{
Print (" ". $ InputText .""); // Print $ inputText
}
Print ("This line is not aggravated!
N "); // Print the string directly
PrintBold ("This line is aggravated !!! "); // Call the function printBold () function
Print ("
N ");
Print ("This line is not aggravated!
N "); // Print the string directly
?>




Functions with return values


Functions with return values



Function makeBold ($ inputText) // defines the function makeBold () function
{
$ BoldedText =" ";
$ BoldedText. = $ inputText;
$ BoldedText. ="
";
Return ($ boldedText); // return the variable $ boldedText
}
Print ("This line is not aggravated !!!
N "); // Print the string directly
Print (makeBold ("This line is aggravated !!! ")."
N "); // call the function makeBold () function
Print ("This line is not aggravated !!!
N "); // Print the string directly
?>




Functions with default parameters


Functions with default parameters



Function printColored ($ Text, $ Color = "black") // defines the function.
{
Print ("$ Text"); // Obtain the content and color of the string.
}
PrintColored ("This is a black color word! "); // Call the function
Print ("

N ");
PrintColored ("This is a blue color word! "," Blue "); // call the function
Print ("
N ");
?>




Identify whether it is an integer by using the delivery algorithm


Judge integer


Function checkInteger ($ Number)
{
If ($ Number> 1)
{
/* The integer minus 1 is still an integer */
Return (checkInteger ($ Number-1 ));
}
Elseif ($ Number <0)
{
/* For a negative number ,*/
/* You can analyze its absolute value */
Return (checkInteger (-1) * $ Number-1); // Obtain the absolute value and analyze the negative Number by an integer.
}
Else
{
If ($ Number> 0) AND ($ Number <1 ))
{
Return ("Of course not ");
}
Else
{
/* 0 and 1 are integers */
/* According to the mathematical definition */
Return ("yes ");
}
}
}
Print (" Is 0 an integer?".
CheckInteger (0 )."
N ");
Print (" Is 7 an integer?".
CheckInteger (7 )."
N ");
Print (" What about 3.5?". CheckInteger (3.5 )."
N ");
Print (" What about-5?". CheckInteger (-5 )."
N ");
Print (" And-9.2?". CheckInteger (-9.2 )."
N ");
?>

Initialize an array


Initialize an array


$ MonthName = array (1 => "January", "February", "March", // Initialize an array
"Maid", "May", "June", "July", "August ",
"September", "October", "November", "December ");
Print ("May in English" is$ MonthName [5].
N "); // Print the 6th elements in the array
?>




Obtain the elements in the array.


Obtain the elements in the array.

$ MonthName = array (
/* Define $ monthName [1] to $ monthName [12] */
1 => "January", "February", "March ",
"Maid", "May", "June ",
"July", "August", "September ",
"October", "November", "December ",
/* Define $ monthName ["Jan"] to $ monthName ["Dec"] */
"Jan" => "January", "Feb" => "February ",
"Mar" => "March", "Apr" => "yml ",
"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 ("Month5Is". $ MonthName [5]."
N ");
Print ("MonthAugIs". $ MonthName [" Aug "]."
N ");
Print ("MonthJuneIs". $ MonthName [" June "]."
N ");
?>



Create a multi-dimensional array


Create a multi-dimensional array

$ Cities = array (// Two-dimensional array ()
"North China" => array (
"Beijing ",
"Tianjin ",
"Shijiazhuang"
),
"Northwest China" => array (
"Xi'an ",
"Lhasa"
)
);
Print ("North China:". $ Cities ["North China"] [0]); // print $ Cities ["North China"] [0]
?>



PHP 4.0 supports table printing


Implement table-like printing


/*
** Table-based data
*/
Print ("













N "); // table startFor ($ Row = 1; $ Row <= 12; $ Row ++){Print (" N "); // start line// Do each columnFor ($ Column = 1; $ Column <= 12; $ Column ++){Print (" ");}Print (" N "); // The end of the row}Print ("
"); // Start column
Print ($ Row * $ Column); // table element product
Print ("
N "); // The end of the table.
?>



View system variables


View PHP environment variables


Print ("the name of the file you are using is :");
Print (_ FILE __);
Print ("
N ");
Print ("");
Print ("your operating system is :");
Print (PHP_ OS );
Print ("");
Print ("Your php version is :");
Print (PHP_VERSION)
?>



Open local or remote files


Open local or remote files


Print ("open file n through http ");
// Open a file through the http protocol
If (! ($ MyFile = fopen ("d: web/php/test/data.txt", "r ")))
{
Print ("file cannot be opened ");
Exit;
}
While (! Feof ($ myFile) // Loop
{
// Read the object content by row
$ MyLine = fgetss ($ myFile, 255 );
Print ("$ myLine
N ");
}
// Close the file handle
Fclose ($ myFile );
?>



Comparison of Methods for opening files


Read file content


// 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 each row of the file while opening the file
If ($ myFile = fopen ("data.txt", "r "))
{
While (! Feof ($ myFile ))
{
$ MyLine = fgets ($ myFile, 255 );
Print ($ myLine );
}
Fclose ($ myFile );
}
?>

/* Open the file and print each row of the file,
Remove the HTML language from the retrieved string.
*/
If ($ myFile = fopen ("data.txt", "r "))
{
While (! Feof ($ myFile ))
{
$ MyLine = fgetss ($ myFile, 255 );
Print ($ myLine );
}
Fclose ($ myFile );
}
?>



Common file access attributes


Common file access attributes




Print ("file owner (UID value ):");
Print (fileowner ("data.txt ")."
");
Print ("file size :");
Print (filesize ("data.txt ")."
");
Print ("file type :");
Print (filetype ("data.txt ")."
");
?>

Call text file content


Call text file content



// Print each line while opening the file
$ MyFile = file ("data.txt ");
For ($ index = 0; $ index <count ($ myFile); $ index ++)
{
Print ($ myFile [$ index]."
");
}
?>




Create a directory function


Create a directory function


If (mkdir ("myDir1", 0777) // create a directory function
{
Print ("directory created successfully"); // Directory created successfully
}
Else
{
Print ("directory creation failed! "); // Directory creation failed
}
?>



Browse directory


Browse directory


// Use the table to browse the directory structure
Print ("















N ");// Create the table headerPrint (" N ");Print (" N ");Print (" N ");Print (" N ");$ MyDirectory = opendir ("."); // Create an operation directory handle// Read every sub-item in the directoryWhile ($ entryName = readdir ($ myDirectory )){Print (" ");Print (" ");Print (" ");Print (" N ");}Closedir ($ myDirectory); // Close the DirectoryPrint ("
File nameFile size
$ EntryName");
Print (filesize ($ entryName ));
Print ("
N ");
?>



PHP information


PHP information


Phpinfo ();
?>



Common numeric judgment functions


Common numeric judgment functions


// Determine the array
$ Colors = array ("red", "blue", "green ");
If (is_array ($ colors ))
{
Print ("colors is an array "."
");
}
// Double-precision data determination
$ Temperature = 15.23;
If (is_double ($ Temperature ))
{
Print ("Temperature is a double "."
");
}
// Integer judgment
$ PageCount = 2234;
If (is_integer ($ PageCount ))
{
Print ("$ PageCount is an integer "."
");
}
// Object judgment
Class widget
{
Var $ name;
Var $ length;
}
$ Thing = new widget;
If (is_object ($ thing ))
{
Print ("thing is an object "."
");
}
// Character judgment
$ Greeting = "Hello ";
If (is_string ($ Greeting ))
{
Print ("Greeting is a string "."
");
}
?>



File upload interface


File upload interface


If ($ UploadAction ){
$ UploadAction = 0;
$ TimeLimit = 60;
/* Set the default time-out period to 30 s. If the value is set to 0, the time-out period is not limited */
Set_time_limit ($ TimeLimit );
If ($ Upfile! = "None ")&&
($ Upfile! = ""))
{
$ Filepath = "d: webwebphptest"; // upload the file storage path
$ FileName = $ Filepath. $ Upfile_name;
If ($ Upfile_size <1024) // upload the file size
{$ FileSize = (string) $ Upfile_size. "byte ";}
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"

N ";
Echo "file $ Upfile_name has been uploaded successfully! ";
Echo"

N ";
Echo "file location: $ FileName ";
Echo"

N ";
Echo "file size: $ FileSize ";
Echo"

N ";
}
Else
{Echo "file $ Upfile_name Upload failed! ";}
}
Else
{Echo "file $ Upfile_name already exists! ";}
}
Else
{Echo "you have not selected any file upload! ";}
Set_time_limit (30); // restores the default timeout value.
}
?>


Html head title classic loop example/TITLE/head body? For ($ counter = 1; $ counter = 6; $ counter ++) // Loop 6 Times {print (Bcounteris $ counter/BBRn); // print 6 times }? /BODY/H...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.