Several classic codes that must be added to favorites when learning php

Source: Internet
Author: User
Several classic loop examples of classic code that must be added to the favorites of php

  Classic loop example   
 Counter is $ counter
\ N "); // Print 6 times}?>

Advanced application of

  Advanced application of   
 How many days are there from Monday?\ N "); print ("
 
 
    \ N "); for ($ currentDate = date (" U "); // defines the $ currentDate time format date (" l ", $ currentDate )! = "Monday"; // determines if the current system time is Monday $ currentDate + = (60*60*24 )) // add 1 day {/*** print time name */print ("
  1. ". Date (" l ", $ currentDate)." \ n ");} print ("
\ N ");?>

Simple function call:

  Simple functions    
 ". $ InputText .""); // Print $ inputText} print (" This line is not aggravated!
\ N "); // Print the string printBold directly (" This line is aggravated !!! "); // Call function printBold () function print ("
\ N "); print (" This line is not aggravated!
\ N "); // Print the string directly?>

Functions with return values

  Functions with return values    
 "; $ BoldedText. = $ inputText; $ boldedText. =""; Return ($ boldedText); // return the variable $ boldedText} print (" This line is not aggravated !!!
\ N "); // print the string print (makeBold (" This line is aggravated !!! ")."
\ N "); // call function makeBold () function print (" This line is not aggravated !!!
\ N "); // Print the string directly?>

Functions with default parameters

  Functions with default parameters    
 
\ N "); printColored (" This is a blue color word! "," Blue "); // call the function print ("
\ N ");?>

Use a regular algorithm to determine whether it is an integer

  Judge integer   
 1) {/* 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); // you can obtain the absolute value, analyze negative numbers by integers} else {if ($ Number> 0) AND ($ Number <1) {return ("Of course not ");} else {/* 0 and 1 are integers * // * according to mathematical definitions */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   
 "January", "February", "March", // Initialize an array "limit L", "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. "January", "February", "March", "Maid", "May", "June", "July", "August", "September", "October ", "November", "December",/* defines $ 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 ("Month5Is". $ MonthName [5]."
\ N "); print (" MonthAugIs". $ MonthName [" Aug "]."
\ N "); print (" MonthJuneIs". $ MonthName [" June "]."
\ N ");?>

Create a multi-dimensional array

  Create a multi-dimensional array  
 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   
 \ N "); // table start for ($ Row = 1; $ Row <= 12; $ Row ++) {print ("\ N "); // start row // do each column for ($ Column = 1; $ Column <= 12; $ Column ++) {print (""); // Start Column print ($ Row * $ Column); // print ("");} Print ("\ N "); // row end} print ("\ N "); // The end of the table?> 

View system variables

  View PHP environment variables   
 \ 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   
 \ N ") ;}// close the file handle fclose ($ myFile) ;?> Comparison of Methods for opening files  Read file content   
  
  
  
  
   

Common file access attributes

  Common file access attributes   
"); Print (" file size: "); print (filesize (" data.txt ")."
"); Print (" file type: "); print (filetype (" data.txt ")."
");?>

Call text file content

  Call text file content   
  
  ") ;}?>
   

Create a directory function

  Create a directory function   
   

Browse directory

  Browse directory   
 \ N "); // create the table header print ("\ N "); print ("File name\ N "); print ("File size\ N "); print ("\ N "); $ myDirectory = opendir (". "); // Create an operation directory handle // read each sub-item in the directory while ($ entryName = readdir ($ myDirectory) {print (""); Print ("$ EntryName"); Print (""); Print (filesize ($ entryName); print (""); Print ("\ N ");} closedir ($ myDirectory); // Close the directory print ("\ N ");?> 

PHP information

  PHP information   
   Common numeric judgment functions  Common numeric judgment functions   
 ");} // Determine the double precision of $ 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 "."
");} // Determine the character $ Greeting =" Hello "; if (is_string ($ Greeting) {print (" Greeting is a string "."
") ;}?>

File upload interface

    File upload interface    
   
  
  
\ N "; echo" file $ Upfile_name uploaded successfully! "; Echo"

\ N "; echo" file location: $ FileName "; echo"

\ N "; echo" file size: $ FileSize "; echo"

\ N ";}else {echo" file $ Upfile_name Upload failed! ";}} The else {echo" file $ Upfile_name already exists! ";}} Else {echo" you have not selected any file upload! ";}Set_time_limit (30); // restore the default timeout settings}?>

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.