Chapter 1 (php Basics) php and mysqlweb development, php and mysql programming
-
Use semicolons (;) to separate php statements
-
Use commas to list function parameters and other items
-
Delimiter characters, such as line breaks, spaces, and tabs are considered as spaces.
-
Connect a string with a dot
-
There are several ways to annotate php:
/* */ // #
- Php is a weak language and does not need to be declared before variables are used.
-
Php can convert variable types
$test = 0;$test1 = (float)$test1;
-
Constants are capitalized. The difference between constants and variables is that they do not have $
-
Php has some super global variables.
- $ GLOBALS all global variable arrays
- $ _ SERVER environment variable array
- $ _ GET: variable array passed to the script through GET
- $ _ POST: variable array passed to the script through POST
- $ _ COOKIE cookie variable array
- $ _ FILES variable array related to file Upload
- $ _ ENV environment variable array
- $ _ REQUEST an array of variables input by all users
- $ _ SESSION variable array
< ?phpforeach($_SERVER as $var => $value){ echo "$var => $value \n";}?>----CR_RUNID => 19006 TERM_PROGRAM => CodeRunner TERM => dumb SHELL => /bin/zsh TMPDIR => /var/folders/_4/6v2frjcx2w90jvq9dh92z1vw0000gn/T/ CR_DEBUGGING => 0 SSH_AUTH_SOCK => /private/tmp/com.apple.launchd.EFcqscDf4y/Listeners filename => Untitled 2.php __CF_USER_TEXT_ENCODING => 0x1F5:0x19:0x34 CR_DEVELOPER_DIR => /Applications/CodeRunner.app/Contents/SharedSupport/Developer CR_FILENAME => Untitled 2.php PATH => /Library/Application PWD => /var/folders/_4/6v2frjcx2w90jvq9dh92z1vw0000gn/T/CodeRunner XPC_FLAGS => 0x0 CR_TMPDIR => /var/folders/_4/6v2frjcx2w90jvq9dh92z1vw0000gn/T/CodeRunner XPC_SERVICE_NAME => 0 SHLVL => 1 ANT_ROOT => /Applications/Cocos/tools/ant/bin CR_VERSION => 3960 LC_CTYPE => UTF-8 COCOS_FRAMEWORKS => /Applications/Cocos/frameworks CR_ENCODING => 4 _ => /usr/bin/php PHP_SELF => Untitled 2.php SCRIPT_NAME => Untitled 2.php SCRIPT_FILENAME => Untitled 2.php PATH_TRANSLATED => Untitled 2.php DOCUMENT_ROOT => REQUEST_TIME_FLOAT => 1454780104.3473 REQUEST_TIME => 1454780104 argv => Array argc => 1
-
Trielement operators
($grade >= 50 ? 'Passed' : 'Failed')
-
Arrays can be accessed using [] or =>
- Run the command in reverse quotes
- You can use new and-> to initialize class instances and class members, or call methods.
- Print is slower than echo because print will return.
- You can use gettype () to test the function type. php also provides other test functions, such as is_array () and is_object ().
- You can use isset () and empty () to test the variable status. The former is used to determine whether the variable is set or not, and the latter is used to determine whether the variable is empty.
-
You can use printf for data formatting and output. similar to the C language, printf supports $ d and $ s.
printf ("aaa is $d.",100);
-
A value assignment is a value assignment variable. the address of each variable in the memory is different. a value assignment by reference means that multiple variables reference the same content.
Value by value $ value1 = "hello"; value by Reference $ value1 = "hello"; $ value2 = & $ value1;
Variable scope
Php variables have four scopes:
1. local variables
The variables declared in the function are local variables and can only be referenced in the function.
$ X = 4; function assignx () {$ x = 0; print ("A result is :". $ x); // although the same name is used, only the internal variable} assignx (); print ("B result is :". $ x); // outside, variables with external scopes will be used ---- A result is: 0. this is the result of assignx () execution. B result is: 4. this is assignx () execution () result of the next row2. function parameters
As the name implies, in the scope of function parameters, function parameters cannot be used after exiting the function.
< ?phpfunction xman($value) { $value = $value * 10; return $value;}$a = xman(10);echo $a;?>---1003. global variables
Global variables can be accessed anywhere in the program. The keyword is global or GLOBALS array.
<? Php $ somevar = 15; function addit1 () {global $ somevar; // if global exists, it becomes a global variable. The global variable will not disappear, so it is 15 + 1 $ somevar ++; echo "somevar1 is ". $ somevar. "\ n" ;}addit1 (); function addit2 () {$ somevar ++; // because it is not a global variable, it is a local variable, because as for the internal function, so it starts from 0, so it is 0 + 1 echo "somevar2 is ". $ somevar. "\ n" ;}addit2 () ;?> ---- Somevar1 is 16somevar2 is 14. static variables
Static variables do not disappear when the function exits. they are similar to global variables, but cannot be accessed anywhere.
<? Phpfunction keep_track () {static $ count = 0; // The calculation result is saved, so it continues to + 1 $ count ++; echo $ count. "\ n";} keep_track ();?> ---- 123
This article was created by Peter yuan and is licensed on the Chinese mainland using the signature-non-commercial use 2.5. You need to contact the author before reprinting and referencing, and sign the author and indicate the source of the article. God-like teenagers» Chapter 1 (php Basics) php and mysql web development, php and mysql programming