PHP Learning (i)

Source: Internet
Author: User
Tags learn php php operator type null alphanumeric characters

The following are from http://www.w3school.com.cn/php/

Thanks to the learning platform provided by W3chool. Praise.

PHP Learning:
Fragments of 1.PHP
<?php>
....
<?>

2. Three ways to annotate:
2.1//...
2.2 # ...
2.3/* ....
...*/

3. function, class, keyword is not sensitive to case
echo = = = Echo

4. All variables are case sensitive
$value! = $valUE

5.PHP Variable rules:
5.1 The variable begins with a $ sign, followed by the name of the variable
5.2 Variable names must begin with a letter or underscore
5.3 Variable names cannot start with a number
5.4 Variable names can only contain alphanumeric characters and underscores (A-Z, 0-9, and _)
5.5 Variable names are case sensitive ($y and $Y are two different variables)

The 6.PHP has three different variable scopes:
6.1 Local (local)
6.2 Global (overall)
6.3 Static (Static)

7.Local and Global Scopes
Variables declared outside of the 7.1 function have Global scope and can only be accessed outside of the function.
7.2 variables declared inside the function have a LOCAL scope and can only be accessed inside the function.

8.PHP Global Keywords
The 8.1 global keyword is used to access global variables within functions. (To do this, use the Global keyword before (inside the function) variable)

9.PHP Static Keywords
Typically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable. Achieving this requires a bit of further work.
To do this, use the static keyword when you first declare the variable.

10.PHP Echo and Print statements
The difference between Echo and print:
echo-capable of outputting more than one string (echo "This", "string", "is", "made", "with multiple parameters.";)
Print-can only output one string and always returns 1 (print "I ' m about to learn php!";)
Tip: Echo is slightly faster than print because it does not return any values.

11.PHP logic is true or false.
$x =true;
$y =false;

12.PHP arrays
An array stores multiple values in a variable.
$cars =array ("Volvo", "BMW", "SAAB");

13.PHP objects
In PHP, you must explicitly declare an object.
First we must declare the class of the object. For this, we use the class keyword. A class is a structure that contains properties and methods.
We then define the data type in the object class, and then use this data type in an instance of the class:
Instance
<?php
Class Car
{
var $color;
function Car ($color = "green") {
$this->color = $color;
}
function What_color () {
return $this->color;
}
}
?>

14.PHP NULL Value
A special NULL value indicates that the variable has no value. Null is the only possible value for the data type NULL.
The null value indicates whether the variable is empty. Also used to differentiate between empty strings and null-valued databases.
You can empty the variable by setting the value to NULL

15. String manipulation functions
15.1 the PHP strlen () function, the strlen () function returns the length of the string, measured in characters.
<?php
Echo strlen ("Hello world!");
?>

15.2PHP Strpos () function
The Strpos () function is used to retrieve the specified character or text within a string.
If a match is found, the first matching character position is returned. If no match is found, FALSE is returned.
<?php
Echo Strpos ("Hello world!", "World"); Output:6
?>

16.PHP Constants
A 16.1 constant is an identifier (name) for a single value. The value cannot be changed in the script.
Valid constant names begin with a character or underscore (there is no $ symbol in front of the constant name).
Unlike variables, constants are automatically global throughout the script.
16.2 Setting PHP Constants
To set a constant, use the Define () function-it uses three parameters:
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) true (insensitive).
<?php
Define ("greeting", "Welcome to w3school.com.cn!");
echo greeting;
?>

17.PHP operator
17.1 subtraction, there is no difference between modulo and other languages.
17.2 String Operators:
1). Thread connection
$txt 1 = "Hello"
$txt 2 = $txt 1.     "World!" Now $txt 2 contains "Hello world!"

2). = String Assignment
$txt 1 = "Hello"
$txt 1. = "world!" Now $txt 1 contains "Hello world!"
17.3 comparison Operators
Comparison operators are basically consistent with other languages. Special several:
= = = Congruent (identical) $x = = = $y returns True if $x equals $y and they are of the same type.
!== is not identical (completely different) $x!== $y Returns True if $x are not equal to $y and they are of the same type.
Code:
$x = 100;
$y = "100";

Var_dump ($x = = $y); Returns true because the values are equal
echo "<br>";
Var_dump ($x = = = $y); Returns false because the types are not equal
echo "<br>";
Var_dump ($x! = $y); Returns false because the values are equal
echo "<br>";
Var_dump ($x!== $y); Returns true because the values are not equal
echo "<br>";

17.4 logical operators
Align with other languages. Note the points:
<===> &&
or <===> | |

17.5 array operators (for comparison of arrays)
+ Union $x + $y $x and $y Union (but not overlapping keys)
= = equals $x = = $y Returns True if $x and $y have the same key/value pair.
= = = Congruent $x = = = $y Returns True if $x and $y have the same key/value pairs, and the same type is the same in order.
! = is unequal $x! = $y Returns True if the $x is not equal to $y.
<> unequal $x <> $y returns True if $x is not equal to $y.
!== not congruent $x!== $y returns True if the $x is completely different from the $y.
17.6 Logic Control, loop
Logical control if Else ElseIf is consistent with other languages.
The loop includes: while do. While for foreach ($arr as $value) is consistent with other languages.
You need to be aware of the use of foreach:
<?php
$colors = Array ("Red", "green", "blue", "yellow");

foreach ($colors as $value) {
echo "$value <br>";
}
?>


18.PHP function
A function is a block of statements that can be reused in a program.
The function does not execute immediately when the page loads.
Functions are executed only when they are called.
The function name is not sensitive to capitalization.
<?php
function Writemsg () {
echo "Hello world!";
}

Writemsg (); Calling functions
?>


Examples of function-passing arguments:
<?php
function Familyname ($fname, $year) {
echo "$fname Zhang. Born in $year <br> ";
}

Familyname ("Li", "1975");
Familyname ("Hong", "1978");
Familyname ("Tao", "1983");
?>

function parameter Default value:
<?php
function SetHeight ($minheight =50) {
echo "The height is: $minheight <br>";
}

SetHeight (350);
SetHeight (); The default value of 50 will be used
SetHeight (135);
SetHeight (80);
?>

19.PHP arrays
19.1 Basic Knowledge
The array () function is used to create arrays.
In PHP, there are three types of arrays:
1) Indexed array-an array with a numeric index
2) Associative array-an array with the specified key
3) Multidimensional array-contains the number of one or more arrays

Indexes are automatically assigned (index starting from 0):
$cars =array ("Volvo", "BMW", "SAAB");
Instance:
<?php
$cars =array ("Volvo", "BMW", "SAAB");
echo "I like". $cars [0]. ", " . $cars [1]. "and". $cars [2]. ".";
?>
19.2 operation of the array
The count (array name) function is used to return the length of the array (number of elements)
Array sorting:
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



19.3 Associative arrays
An associative array is an array that uses the specified key that you assign to the array.
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Or:
$age [' Peter ']= ' 35 ';
$age [' Ben ']= ' 37 ';
$age [' Joe ']= "43";
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
echo "Peter is". $age [' Peter ']. "Years old.";
?>
Iterate through all the values in the output associative array:
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");

foreach ($age as $x = + $x _value) {
echo "key=". $x. ", value=." $x _value;
echo "<br>";
}
?>


20. Super Global variables
Many of the pre-defined variables in PHP are "hyper-global",
This means that they are available in all scopes of a script. There is no need to perform a global $variable in a function or method; You can access them.
These hyper-global variables are:
$GLOBALS
$_server
$_request
$_post
$_get
$_files
$_env
$_cookie
$_session

Example 1: About $globals
<?php
$x = 75;
$y = 25;

function addition () {
$GLOBALS [' z '] = $GLOBALS [' x '] + $GLOBALS [' Y '];
}

addition ();
Echo $z; 100
?>
Learn the rest of the super global variables later.

PHP Learning (i)

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.