Basic syntax
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<?php
Statement
Branch statements
$a = 5;
if ($a = = 5)
{
echo "equal";
}
Else
{
echo "Unequal";
}
Switch ($a)
{
Case 1:
echo "1111";
Break
Case 2:
echo "2222";
Break
Default
echo "Default";
Break
}
Looping statements
for ($i =0; $i <10; $i + +)
{
echo $i. " <br> ";
}
$a = 10;
while ($a >0)
{
echo $a. " <br> ";
$a--;
}
public int Show (int a)
{
}
Function four elements: return type, function name, parameter, function body
How functions are defined
function Show ($a)
{
echo "Hello";
}
Show ();
function with parameters
function Show ($a, $b)
{
echo $a + $b;
}
Show (3,4,5);
Functions with default values
function Show ($a =5, $b =5)
{
echo $a + $b;
}
Show (3,2);
Functions with variable parameters
function Show ()
{
$attr = Func_get_args ();//Gets the parameters of the function
$sum = 0;
for ($i =0; $i <count ($attr), $i + +)//func_num_args () Gets the number of arguments
{
$sum = $sum + $attr [$i];
}
Echo $sum;
}
Show (1,2,3,4);
Functions that have return values
function Show ()
{
Return "AAA";
}
Echo Show ();
Date_default_timezone_set (' PRC ')
Common functions
echo rand (0,100);//random number generation function
echo Time (); Take the current date time
echo Date ("Y-m-d h:i:s", Time ());//Format datetime
$a = Strtotime ("2016-5-6");//replace DateTime format with timestamp
String handling functions
$a = "Hello";
echo strlen ($a);//return string length
echo strcmp ("Hello", "hello");//Compare two strings for equality, case-sensitive
echo strcasecmp ("Hello", "hello");//Compare two strings for equality, case insensitive
Echo strtolower ("HELLO");//convert string to lowercase
echo strtoupper ("Hello");//convert String to uppercase
$str = "Hello|world|join|on";
Explode ("|", $STR);//split string, return array
Var_dump (Explode ("|", $str));
$attr = Array ("AAA", "BBB", "CCC");
echo Implode ("|", $attr);//stitching an array into a string
Echo Substr_replace ($STR, "AAA", 0,5);//replace string (a location)
echo str_replace ("L", "a", $str);//Find and replace
Echo substr ($str, 0,5);//Intercept string
Some small knowledge
Both single and double quotation marks can define a string
$a = "join";
$s 1 = "hello\" {$a}world ";
1. You can use the escape character inside the double quotation mark, the single quotation mark cannot use the original output
2. Double quotation mark inside can explain variable, single quotation mark not
echo $s 1. " <br> ";
$s 2 = ' hello\ ' {$a}world ';
Echo $s 2;
Defining a string (block)
$str = <<<str
<div style= "background-color:red; Color:white; width:100px;height:30px ">hello</div>
<div style= "background-color:red; Color:white; width:100px; height:30px ">world</div>
STR;
?>
</body>
PHP Lesson Two