php basic syntax
The php script executes on the server and sends the pure HTML results back to the browser
Basic grammar
- Beginning and end
<?php ?>
- The file extension is. php
- Each line of code must end with a semicolon
php output
- echo and print, which can contain HTML tags
echo: you can output one or more strings
print: only one string is allowed to be output, and the return value is always 1
$a=1; $str="Hello"; #This output variable must be followed by a space, otherwise an error will be reported echo "$str World!$a <br>"; #==There is no need to add a space for this echo "{$str} World!{$a} <br>"; #Multiple strings, using, $str2="I love php!"; echo "This"," is"," my first"," php process.",$str2,"<br>"; #Connect multiple strings, using echo $str.$str2." ".$a;
php comments
- Single line / / or#
- Multiline / **/
- Document comments / * **/
#No output //No output /* No output */ /** * No output */
php variable
- Define variable: $variable name = variable value
$a = 1; echo $a; $b = 2; $c = 3; echo $b; echo $c;
matters needing attention:
- To be expressed in $
- Start with a letter or underscore
- Variable names can only consist of letters, numbers, and underscores
- Cannot contain spaces, separated by underscores
- Variable names are case sensitive
- Keywords cannot be used as variable names
php variable scope
php has three different variable scopes
- Local (local)
- Global
- Static
local and global
- Variables declared outside the function have global scope and can only be accessed outside the function
- Variables declared within a function have local scope and can only be accessed inside the function
**Example:**
$a=100; function demoTest(){ $b=50; echo "<p>Function internal test</p>"; echo "variable a=".$a."<br>"; echo "variable b=".$b; } demoTest(); echo "<p>Function external test</p>"; echo "variable a=".$a,"<br>"; echo "variable b=".$b; /* *Function internal test *Variable a= *Variable b=50 *Function external test *Variable a=100 *Variable b= */ #External can only access external, internal can only access internal
Use the global keyword to access global variables, or use $GLOBALS ['variable name']. This array stores all global variables and can be accessed inside and outside
$x=100; $y=50; $t=0; function Test(){ global $x,$y; $y+=$x; $GLOBALS['t']=$GLOBALS['x']+$GLOBALS['y']; } Test(); echo $y; //150 echo "<br>".$t; //=100+150
static
The static modified variable will be retained after the calling function is executed, but it is still a local variable
function Test(){ static $x=0; echo $x."<br>"; $x++; } Test(); //0 Test(); //1 Test(); //2 Test(); //3
php data type
php is a weakly typed language and does not need to specify data types
Four scalar types
- character string
- Integers (three: decimal, hexadecimal and octal) are similar to Java and lower case
- Floating point number
- Boolean (only true and false)
$x=10; $y=0xABCDE; $m=07777; //var_dump returns the data type and value var_dump($x); //int(10) $n=10.5; var_dump($n); // float(10.5) $flag=true; var_dump($flag); //bool(true)
other
-
array
-
object
-
null
Set the value to null and empty the variable
php operator
Arithmetic operator
+,-,*,/,%
Assignment Operators
=,+=,-=,*=,/=,%=
String operator
$str1="Hello"; echo $str1." World!"; //Hello World! echo "<br>"; $str1.=" World!"; echo $str1; //Hello World!
- . concatenation is equivalent to Java string addition+
- . = serial assignment is equivalent to+=
Increment / decrement operator
- $x++ , ++$x
- $x-- , --$x
Comparison operator
==,!=,>,<,<=,>=
Extension:
- =Congruent (identical), $x$y and equal types return true
- < > not equal to
- !=== Incomplete (completely unequal). If $x is not equal to $y, or the type is different, return true
$x=5; $y=5; $z="5"; var_dump($x===$y); //true echo "<br>"; var_dump ($x!==$z); //true
Logical operator
&& and , || or , !
XOR (exclusive or) $x and $y have only one true value, and return true
Array Operations
Used to compare arrays
+Union, = = equal, = congruent (same order)= Not equal to, < > not equal to! Incomplete (completely different)
php constant
Cannot change the value of. Constants are automatically global
Use the define() function
Three parameters:
- The name of the constant
- Constant value
- Specifies whether the constant name is case sensitive. The default value is false
define("GOOGLE","I'm google."); echo GOOGLE; echo "<br>"; define("MICRO","I'm Microsoft"); function Test(){ echo MICRO; } Test(); echo "<br>"; echo MICRO;
php string function
Common string operation functions
- php strlen() function
Length of returned string = Java: str.length();
- php str_word_count()
Counts the words in the string
- php strrev()
Reverse string
- php strpos()
Retrieves the specified character or text within a string
If found, the first matching character position will be returned; if not found, false will be returned
- php str_replace()
Replace some characters in the string with some strings
Three parameters: 1. Find value 2. Replace value 3. Search string
$str="this is php"; echo strlen($str)."<br>"; //11 echo str_word_count($str),"<br>"; //3 echo strrev($str)."<br>"; echo strpos($str,"is"); //2. Index 2 appears is for the first time echo "<br>"; echo str_replace("php","Java",$str); //this is Java