PHP Introduction need to master several of the functional Code _php tutorial

Source: Internet
Author: User
Tags define function php introduction
Classic Loop examples


<title>Classic Loop examples</title>


for ($counter = 1; $counter <= 6; $counter + +)//cycle 6 times
{
Print (" counter is $counter
n "); Print 6 times
}
?>



for advanced use


<title>For advanced use</title>


/*
* * Print the necessary explanatory text
*/
Print (" How many days is it from Monday? n ");
Print ("
      n ");
      for ($currentDate = Date ("U");//define $currentdate time format
      Date ("L", $currentDate)! = "Monday"; Determine if the current system time is Monday
      $currentDate + = (60 * 60 * 24))//Current time plus 1 days
      {
      /*
      * * Print time name
      */
      Print ("
    1. " . Date ("L", $currentDate). "N");
      }
      Print ("
n ");
?>



simple invocation of the function:


<title>A simple function</title>



function Printbold ($inputText)//define function Printbold ()
{
Print (" " . $inputText. ""); Print $inputtext
}
Print ("This line is not aggravating!")
n "); Print a string directly
Printbold ("This line is aggravating!!! "); Call function Printbold () functions
Print ("
n ");
Print ("This line is not aggravating!")
n "); Print a string directly
?>




functions that have return values


<title>Functions that have return values</title>



function Makebold ($inputText)//define Functions Makebold ()
{
$boldedText = " ";
$boldedText. = $inputText;
$boldedText. = "
";
return ($boldedText); Return variable $boldedtext
}
Print ("This line does not aggravate!!!"
n "); Print a string directly
Print (Makebold ("This line is aggravated!!! ") . "
n ");//Call Function Makebold ()
Print ("This line does not aggravate!!!"
n "); Print a string directly
?>




functions with default parameters


<title>Functions with default parameters</title>



function printcolored ($Text, $Color = "BLACK")//define function functions
{
Print ("$Text"); Get the contents and colors of a string
}
Printcolored ("This is the black color of the word!") "); Calling function functions
Print ("

n ");
Printcolored ("This is the blue color of the word!") "," Blue "); Calling function functions
Print ("
n ");
?>




using a recursive algorithm to determine whether an integer


<title>Judging integers</title>


function Checkinteger ($Number)
{
if ($Number > 1)
{
/* integer minus 1 is still an integer */
Return (Checkinteger ($Number-1));
}
ElseIf ($Number < 0)
{
/* For a negative number, */
/* You can analyze the absolute value of it */
Return (Checkinteger (( -1) * $Number-1));//absolute value, negative number by integer analysis
}
Else
{
if (($Number > 0) and ($Number < 1))
{
Return ("Certainly not");
}
Else
{
/* 0 and 1 are integers */
/* According to the relevant 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 ");
?>

initializing an array


<title>Initializing an array</title>


$monthName = Array (1=> "January", "February", "March",//initialization of an array
"April", "may", "June", "July", "August",
"September", "October", "November", "December");
Print ("May in English" is$monthName [5]
n ");//Print the 6th element in the array
?>




gets the elements in the array


<title>Gets the elements in the array</title>

$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" and "April",
"May" = "may", "June" and "June",
"Jul" = "July", "the August" and "the"
"Sep" = "September", "Oct" = "October",
"Nov" = "November", "Dec" and "December",
/* Define $monthname["Jan"] to $monthname["Dec"]*/
"January" = "January", "February" and "February",
"March" = "March", "April" and "April",
"May" = "may", "June" and "June"
"July" = "July", "August" and "August",
"September" = "September", "October" and "October",
"November" = "November", "December" and "December"
);
/* Print related elements */
Print ("Month5Is" . $monthName [5]. "
n ");
Print ("Month theIs" . $monthName ["the"]. "
n ");
Print ("MonthJuneIs" . $monthName ["June"]. "
n ");
?>



Create a multidimensional array


<title>Create a multidimensional array</title>

$Cities = Array (//two-D arrays Array ()
"North China" =>array (
"Beijing",
"Tianjin",
Shijiazhuang
),
"Northwest" =>array (
"Xian",
Lhasa
)
);
Print ("North China:". $Cities ["North China"][0]); Print $cities["North China"][0]
?>



PHP 4.0 for tabular printing


<title>Implement table-like printing</title>


/*
* * Data table format
*/
Print ("













n "); The form starts for ($Row =1; $Row <=; $Row + +) {print (" n "); Start line//Do all column for ($Column =1; $Column <=; $Column + +) {print (" "); } print (" n "); End of Line} print ("
");//Start column
Print ($Row * $Column);//TABLE element product
Print ("
n "); End of table
?>



view some of the system's variables


<title>View the environment variables for PHP</title>


Print ("You are using the file name as:");
Print (__file__);
Print ("
n ");
Print ("");
Print ("Your operating system is:");
Print (Php_os);
Print ("");
Print ("Your PHP version is:");
Print (php_version)
?>



open a local or remote file


<title>Open a local or remote file</title>


Print ("

Open file via HTTP protocol

n ");
Open 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 a file by line
$myLine = FGETSS ($myFile, 255);
Print ("$myLine
n ");
}
Closing a handle to a file
Fclose ($myFile);
?>



several ways to open a file compare


<title>Read File contents</title>


Open a file to print each character of a file at the same time
if ($myFile = fopen ("Data.txt", "R"))
{
while (!feof ($myFile))
{
$myCharacter = fgetc ($myFile);
Print ($myCharacter);
}
Fclose ($myFile);
}
?>

Open a 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);
}
?>

/* Open the file while printing each line of the file,
Also 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);
}
?>



accessing file Common Properties


<title>Accessing file Common Properties</title>




Print ("Owner of File (UID value):");
Print (Fileowner ("Data.txt"). "
");
Print ("Size of File:");
Print (FileSize ("Data.txt"). "
");
Print ("Type of File:");
Print (FileType ("data.txt"). "
");
?>

invoke text file contents


Invoke text file contents



Open a file and print each line
$myFile = File ("Data.txt");
for ($index = 0; $index < count ($myFile); $index + +)
{
Print ($myFile [$index]. "
");
}
?>




Create a directory function


<title>Create a directory function</title>


if (mkdir ("MyDir1", 0777))//function to create the directory
{
Print ("Directory creation succeeded"); Directory established successfully
}
Else
{
Print ("Directory Setup failed!"); Directory Setup failed
}
?>



Browse Catalogs


<title>Browse Catalogs</title>


Navigating the structure of a directory using a table
Print ("















n "); Create the header print for the table (" n "); Print (" n"); Print (" n"); Print (" n "); $myDirectory = Opendir ("."); Create a handle to the operation directory//read out every subkey in the directory while ($entryName = Readdir ($myDirectory)) {print (" "); Print (" "); Print (" "); Print (" n "); } closedir ($myDirectory); Close the catalog print ("
filenamesize of File
$entryName");
Print (FileSize ($entryName));
Print ("
n ");
?>



PHP Related Information


<title>PHP Related Information</title>


Phpinfo ();
?>



commonly used numerical judgment function


<title>Commonly used numerical judgment function</title>


Judging arrays
$colors = Array ("Red", "Blue", "green");
if (Is_array ($colors))
{
Print ("Colors is an array". "
");
}
Double Precision number judgment
$Temperature = 15.23;
if (is_double ($Temperature))
{
Print ("Temperature is a double". "
");
}
Integer judgments
$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


<title>File Upload Interface</title>


if ($UploadAction) {
$UploadAction = 0;
$TimeLimit = 60;
/* Set the time-out limit to the default time of 30s, set to 0 for unlimited time */
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. "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 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 did not select any file upload! "; }
Set_time_limit (30); Restore default timeout settings
}
?>


http://www.bkjia.com/PHPjc/371530.html www.bkjia.com true http://www.bkjia.com/PHPjc/371530.html techarticle Classic Loop Example 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.