PHP Basic Syntax

Source: Internet
Author: User
Tags echo command php script alphanumeric characters

PHP syntax
The PHP script can be placed anywhere in the document.
<?php
echo "Hello world!";
?>
PHP variables
PHP Case Sensitive
In PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive.
In PHP, however, all variables are case-sensitive.
PHP Variable rules:
The variable starts with the $ sign, followed by the name of the variable
The variable name must start with a letter or an underscore
The variable name cannot start with a number
? variable names can only contain alphanumeric characters and underscores (A-Z, 0-9, and _)
Variable names are case sensitive ($y and $Y are two different variables)
PHP is a loosely-typed language
In the example above, please note that we do not have to tell the data type of the PHP variable.
PHP automatically converts the variable to the correct data type based on its value.
PHP has three different scope of variables:
? Local (partial)
? Global (overall)
? static
Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.
Variables declared inside a function have a local scope and can only be accessed inside the function.

PHP Echo and Print statements

The difference between Echo and print:
? Echo-capable of outputting more than one string
? print-You can only output one string and always return 1, which is slow.
echo command to display different strings (also note that the string can contain the HTML tag echo ' lamaqin<br> ', which directly contains the variable):
<?php
function Familyname ($fname, $year) {
echo "$fname Zhang. Born in $year <br> ";
}

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

PHP data type
The string can be any text within quotation marks. You can use single or double quotation marks.

The

PHP string function
strlen () function returns the length of a string, measured in characters. <?php Echo strlen ("Hello world!");? > returns 12. The
Strpos () function is used to retrieve the specified character or text within a string. <?php Echo Strpos ("Hello world!", "World");? > Return 6[index starting from 0]
PHP string Reference Manual [PHP 5 string function]
--
Echo () outputs one or more strings. The
printf () output formatted string [participates in function example definition and usage].
Explode () breaks the string into arrays.
Implode () returns a string that is composed of array elements. The alias of the
Join () implode ().
Lcfirst () converts the first character of a string to lowercase.
Chop () removes white space characters or other characters to the right of the string.
LTrim () removes white space characters or other characters to the left of the string.
Trim () removes white space characters and other characters on either side of the string
RTrim () removes white space characters or other characters to the right of the string. The
sprintf () writes the formatted string to the variable.
Str_replace () replaces some characters in the string (case sensitive). The
Str_split () splits the string into the array.
Strtolower () converts the string to lowercase letters.
Strtoupper () converts the string to uppercase.
Str_replace () replaces some characters in the string (case sensitive).
Str_split () splits the string into the array
strlen () returns the length of the string.

--php Var_dump () returns the data type and value of the variable:--
<?php
$x = 5985;
Var_dump ($x);
echo "<br>";
$x =-345; Negative
Var_dump ($x);
echo "<br>";
$x = 0x8c; Hexadecimal number
Var_dump ($x);
echo "<br>";
$x = 047; Octal number
Var_dump ($x);
?>
-----------
PHP Constants [Define (' name ', ' QBBM '), false]
Constants are similar to variables, but constants cannot be changed or revoked once defined.
A 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).
Note: Unlike variables, constants are automatically global throughout the script.
Set 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.
PHP operators
PHP string Operators

Operator name Example Results
. Threaded $txt 1 = "Hello" $txt 2 = $txt 1.  "World!" Now $txt 2 contains "Hello world!"
. = String Assignment $txt 1 = "Hello" $txt 1. = "world!" Now $txt 1 contains "Hello world!"
=
Commonly used for arrays of key = = values
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");

foreach ($age as $x = + $x _value) {
echo "key=". $x. ", value=." $x _value;
echo "<br>";
}
? >[variables to be defined outside the string?]
-
Object properties and objects---object methods commonly used for objects

PHP functions
The real power of PHP comes from its function: it has more than 1000 built-in functions.
To return a value to a function, use the return statement:
PHP arrays
$cars =array ("Volvo", "BMW", "SAAB");
The count () function returns the length of the array (number of elements):
PHP Array Sorting
? sort ()-sort the array in ascending order
? Rsort ()-Sort in descending order of an array
? Asort ()-sorts associative arrays in ascending order based on values
? Ksort ()-Sort associative arrays in ascending order by key
? Arsort ()-sorts associative arrays in descending order by value
? Krsort ()-sorts associative arrays in descending order by key
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Sort ($cars);
?>
PHP Global Variables-Super global variables
Many of the predefined variables in PHP are "hyper-global", which means 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
<?php
$x = 75;
$y = 25;

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

addition ();
Echo $z;
?>

PHP Basic Syntax

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.