No | Category | Code Examples | Test Code | Web Link | Remarks |
---|---|---|---|---|---|
1 | check what browser the user is accessing your web server | 0 | |||
2 | closing tag ?> can be omitted | ||||
3 | Three types of comments | ||||
4 | 4 scalar types | ";
// 2. integer:
$anInt = 8;
echo "anInt == " . $anInt . " "; // 3. float: $aFloat = 3.14159; echo "aFloat scalar == " . $aFloat . " "; // 4. string: $aString = "Hello World"; echo "aString scalar == " . $aString . " "; |
|||
5 | 2 compound types | "bar", 12 => true);
echo "arr[foo] == " . $arr["foo"] . " "; // bar echo "arr[12] == " . $arr[12] . " "; // 1 print_r($arr); // 2. object type: class foo { function do_foo() { echo "Doing foo. "; } } $bar = new foo; $bar->do_foo(); print_r($bar); print(" "); var_dump($bar); |
|||
6 | 2 special types | ||||
7 | some pseudo-types (mixed) | ";
}
echo " "; // str_replace() will accept strings and arrays. // Provides: $bodytag = str_replace("%body%", "black", "body text='%body%'>"); echo $bodytag . " "; // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); print_r($onlyconsonants); echo " "; // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "meat", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); print($newphrase); |
|||
8 | some pseudo-types for readability reasons | http://www.php.net/manual/en/language.pseudo-types | |||
9 | type check | "; // prints out: boolean
echo gettype($a_str) . " "; // prints out: string // If this is an integer, increment it by four if (is_int($an_int)) { $an_int += 4; echo $an_int; } echo " "; // If $a_bool is a bool, print it out // (does not print out anything) if (is_bool($a_bool)) { echo "Boolean: $a_bool"; } |
|||
10 | boolean | http://www.php.net/manual/en/language.types.boolean.php | |||
11 | integers | http://www.php.net/manual/en/language.types.integer.php | |||
12 | Floating point numbers | http://www.php.net/manual/en/language.types.float.php | |||
13 | Strings | '; $str = << |
http://www.php.net/manual/en/language.types.string.php | ||
14 | String Operators | ";
echo $b . " "; $anInt = 12; echo strval($anInt); |
http://www.php.net/manual/en/language.operators.string.php | ||
15 | String Functions - 1. addcslashes -- Quote string with slashes in a C style | http://www.php.net/manual/en/function.addcslashes.php | |||
16 | String Functions - 2. addslashes -- Quote string with slashes | http://www.php.net/manual/en/function.addslashes.php | |||
17 | String Functions - 3. bin2hex -- Convert binary data into hexadecimal representation | "; echo hex2bin('48656c6c6f'); // result: Hello | http://www.php.net/manual/en/function.bin2hex.php | ||
18 | String Functions - 4. chop -- Alias of rtrim() | ";
echo strlen(chop("Hello \n")) . " "; echo strlen(rtrim("Hello \t")) . " "; |
http://www.php.net/manual/en/function.chop.php | ||
19 | String Functions - 5. chr -- Return a specific character | '; } echo "•"; # bullet • echo "–"; # en dash echo "—"; # em dash echo "™"; # trademark echo "©"; # copyright mark echo "®"; # registration mark | http://www.php.net/manual/en/function.chr.php | ||
20 | String Functions - 6. chunk_split -- Split a string into smaller chunks | http://www.php.net/manual/en/function.chunk-split.php | |||
21 | String Functions - 7. convert_cyr_string -- Convert from one Cyrillic character set to another | http://www.php.net/manual/en/function.convert-cyr-string.php | |||
22 | String Functions - 8. convert_uudecode -- Decode a uuencoded string | http://www.php.net/manual/en/function.convert-uudecode.php | |||
23 | String Functions - 9. convert_uuencode -- Uuencode a string | http://www.php.net/manual/en/function.convert-uuencode.php | |||
24 | String Functions - 10. count_chars -- Return information about characters used in a string | $val) { echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n"; } print_r(count_chars($data, 1)); echo ord('T'); echo chr(84); | http://www.php.net/manual/en/function.count-chars.php | ||
25 | String Functions - 11. crc32 Calculates the crc32 polynomial of a string | http://www.php.net/manual/en/function.crc32.php | |||
26 | String Functions - 12. crypt One-way string hashing | "; } echo $password; | http://www.php.net/manual/en/function.crypt.php | ||
27 | String Functions - 13. echo Output one or more strings | http://www.php.net/manual/en/function.echo.php | |||
28 | String Functions - 14. explode Split a string by string | http://www.php.net/manual/en/function.explode.php | |||
29 | String Functions - 15. fprintf Write a formatted string to a stream | http://www.php.net/manual/en/function.fprintf.php | |||
30 | String Functions - 16. get_html_translation_table — Returns the translation table used by htmlspecialchars() and htmlentities() | & Krämer"; $encoded = strtr($str, $trans); echo $encoded; // this did not work -- jli | http://www.php.net/manual/en/function.get-html-translation-table.php | ||
31 | String Functions - 17. hebrev Convert logical Hebrew text to visual text | http://www.php.net/manual/en/function.hebrev.php | |||
32 | String Functions - 18. hebrevc Convert logical Hebrew text to visual text with newline conversion | http://www.php.net/manual/en/function.hebrevc.php | |||
33 | String Functions - 19. hex2bin Converts the hex representation of data to binary | http://www.php.net/manual/en/function.hex2bin.php | |||
34 | String Functions - 20. html_entity_decode Convert all HTML entities to their applicable characters | dog now\n"; $a = htmlentities($orig); $b = html_entity_decode($a); echo $a; // I'll "walk" the <b>dog</b> now echo $b; // I'll "walk" the dog now | http://www.php.net/manual/en/function.html-entity-decode.php | ||
35 | String Functions - 21. htmlentities Convert all applicable characters to HTML entities | bold"; // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str); // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str, ENT_QUOTES); // this does not really work -- jli | http://www.php.net/manual/en/function.htmlentities.php | ||
36 | String Functions - 22. htmlspecialchars_decode Convert special HTML entities back to characters | this -> "'; echo htmlspecialchars_decode($str); | http://www.php.net/manual/en/function.htmlspecialchars-decode.php | ||
37 | String Functions - 23. htmlspecialchars Convert special characters to HTML entities | Test"); echo $new; // <a href='test'>Test</a> | http://www.php.net/manual/en/function.htmlspecialchars.php | ||
38 | String Functions - 24. implode Join array elements with a string | "; // lastname,email,phone // Empty string when using an empty array: var_dump(implode('hello', array())); // string(0) "" | http://www.php.net/manual/en/function.implode.php | ||
39 | String Functions - 25. join Alias of implode() | http://www.php.net/manual/en/function.join.php | |||
40 | String Functions - 26. lcfirst Make a string's first character lowercase | ";
echo $bar, " "; |
http://www.php.net/manual/en/function.lcfirst.php | ||
41 | String Functions - 27. levenshtein Calculate Levenshtein distance between two strings | http://www.php.net/manual/en/function.levenshtein.php | |||
42 | String Functions - 28. localeconv Get numeric formatting information | http://www.php.net/manual/en/function.localeconv.php | |||
43 | String Functions - 29. ltrim Strip whitespace (or other characters) from the beginning of a string | http://www.php.net/manual/en/function.ltrim.php | |||
44 | String Functions - 30. md5_file Calculates the md5 hash of a given file | http://www.php.net/manual/en/function.md5-file.php | |||
45 | String Functions - 30. md5_file Calculates the md5 hash of a given file | http://www.php.net/manual/en/function.md5-file.php | |||
46 | PHP New Version Reference | 1) string gettype ( mixed $var ); // return variable type 2) /* associative array */ $row = $result->fetch_array(MYSQLI_ASSOC); printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); 3) if(!isset($_SESSION['username'])){ // go to login page code here. // in login page, store data into session, use following: $_SESSION['username']="username"; } else{ $username=$_SESSION['username'];// get previous session and go your project page } session_register() function has been deprecated since PHP5.3 |