PHP Basic Learning Notes

Source: Internet
Author: User

<! DOCTYPE html>

<body>
<?php
echo "Hello world!";
?>
</body>

PHP's Hello world.
After entering the Chinese language after the garbled situation, Baidu is a bit of coding problem, in the PHP code to add the header ("content-type:text/html; Charset=utf-8 "), after the resolution, after I modify the php.ini, find Default_charset to modify the default encoding, after restarting the computer to take effect, now the default encoding becomes Utf-8.

<! DOCTYPE html>

<body>
<?php
echo "Hello world!";
echo "First PHP";
?>
</body>

? Three ways to Annotate://#/**/
? PHP is insensitive to user-defined functions, classes, and keyword sizes. However, the variable name is case sensitive.
? PHP does not need to specify the data type when defining variables, the system will automatically convert. Three scopes: 1.local (local) 2.global (global) 3.static (Static)
PHP also stores all global variables in an array of $globals[index]. The subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly.
<?php
$x = 5;
$y = 10;
function MyTest () {
$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y '];
}
MyTest ();
Echo $y;
?>
After the function finishes executing, all variables are deleted. However, sometimes I need to not delete a local variable.
<?php
function MyTest () {
static $x = 0;
echo $x;
$x + +;
}
MyTest ();
MyTest ();
MyTest ();
?>
echo vs Print difference: 1.echo-can output more than one string 2.print-can only output one string and always return 1
Echo is slightly faster than print, thinking he doesn't have to return any values.
<?php
echo "echo "Hello world<br>";
echo "I ' m about to learn php!<br>";
echo "This", "string", "is", "made", "with multiple parameters (many parameters)";
?>
Display variables:
<?php
$txt 1= "Learn PHP";
$txt 2= "W3School.com.cn";
$cars =array ("Volvo", "BMW", "SAAB");

echo $txt 1. " <br> ";//use. And, to concatenate strings
echo "Study PHP at $txt 2 <br>";
echo "My car is a $cars [0]";
?>
The print statement is the same as above, but the difference is that you cannot use it to connect, but you can use it. The connection is counted as a string.
<?php
$txt 1= "Learn PHP";
$x =print $txt 1. " 112233 "." SSS "." <br> ";
Print $x;
?>
The Var_dump () method is to determine the type and length of a variable, and to output the value of a variable if the variable has a value for the output of the variable and returns the data type.
PHP String Functions
1.strlen () returns the string length echo strlen ("Hello world!");
2.strpos () is used to retrieve characters or text within a string, and returns a character position if found, or false if not found.
Constants: Constants are preceded by a $ symbol, and are automatically global. Three parameters for define ():
1. Name of the first parameter definition constant
2. The second parameter defines the value of a constant
3. The optional third parameter specifies whether the constant name is case-sensitive. The default is False (sensitive).
Define ("abc", "dddd");
ECHO ABC;
Operator:/floating-point division,% modulo.
= = Equals (same value) = = = Congruent (same type, same order in array)!== not congruent (completely different)
foreach applies only to array traversal.
<?php
$colors =array ("Red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Gets the array length count () function
<?php
$cars =array ("Volvo", "BMW", "SAAB");
echo count ($cars);
?>
An associative array is an array that uses the specified key assigned to the array.
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
echo "Peter is". $age [' Peter ']. "Years-old.";
?>
Loop with foreach
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
foreach ($age as $x = = $xage) {
echo $x. "Name is" $xage. "Years old.<br>";
}
?>
Array sort function
Sort ()-sorts an array in ascending order
Rsort ()-sorts the array in descending order
Asort ()-sorts associative arrays in ascending order based on values
Ksort ()-Sorts the associative array in ascending order, based on the key
Arsort ()-sorts associative arrays in descending order by value
Krsort ()-Sorts the associative array in descending order, based on the key
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Sort ($cars);
foreach ($cars as $a) {
echo "$a <br>";
}
?>
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Asort ($age);//Sort by value
foreach ($age as $x = + $x _key) {
echo "$x, $x _key<br>";
}
?>

PHP Basic Learning Notes

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.