<?php
echo "Hello, world!";
?>
Run Result:
Hello, world!.
The variable is marked "$". You can also put the "Hello, world!" above Write as the following code:
<?php
$message = "Hello, world!";
Echo $message;
?>
String concatenation is the use of the "." (an English period) marked out; other symbols of numbers are the same as you would expect:
<?php
$greeting = "Hello";
$num = 3 + 2;
$num + +;
echo "$greeting $num people!";
?>
Run Result:
Hello 6 people!
PHP has a complete operator that functions as you expect-especially if you have a knowledge background of C or C + +. A good rule of thumb with PHP is: "If you have a problem, try it first, and you'll probably succeed." ”
As in Perl, a string is enclosed in double quotes, which causes the variable to be replaced by a value, but not if enclosed in single quotes. Therefore, the following code:
<?php
$name = ' Susannah ';
$greeting _1 = "Hello, $name!";
$greeting _2 = ' Hello, $name! ';
echo "$greeting _1\n";
echo "$greeting _2\n";
?>
Run Result:
Hello, susannah!.
Hello, $name!
Note that "\ n" in the string is a newline symbol, just like in Perl or C. However, this is only valid for strings enclosed in double quotes.
Variable
PHP can use environment variables as generic variables. This includes the environment variables set by the server for a CGI program (even when you execute PHP in a modular way). Therefore, if the page http://www.domain.com/farm/cattle/cow-cow.cow.html includes the following code:
<?php
echo "[$REQUEST _uri]";
?>
It will output [/farm/cattle/cow-cow-cow.html]
Array
Use square brackets ([and]) to set the index of the array (general or associative):
$fruit [0] = ' banana ';
$fruit [1] = ' papaya ';
$favorites [' animal '] = ' turtle ';
$favorites [' monster '] = ' cookie ';
If you assign a value to an array, but the index is blank, PHP places the object at the end of the list. The above declaration of variable $fruit is the same as the result of the following code:
$fruit [] = ' banana ';
$fruit [] = ' papaya ';
You can also use multidimensional arrays:
$people [' David '] [' shirt '] = ' blue ';
$people [' David '] [' car '] = ' minivan ';
$people [' Adam '] [' shirt '] = ' white ';
$people [' Adam '] [' car '] = ' sedan ';
An easy way to create an array is to
Array ()
function is:
$fruit = Array (' banana ', ' papaya ');
$favorites = Array (' Animal ' => ' turtle '),
' Monster ' => ' cookies);
Or
$people = Array (' David ' => array (' Shirt ' => ' blue '),
' Car ' => ' minivan '),
' Adam ' => array (' Shirt ' => ' white '),
' Car ' => ' sedan '));
The built-in function count () indicates how many elements are in an array:
$fruit = Array (' banana ', ' papaya ');
Print count ($fruit);
Get the following results
2
Control structure
You can use a loop structure such as for and while:
for ($i = 4; $i < 8; $i + +) {
Print "I have eaten $i bagels today.\n"; }
Results
I have eaten 4 bagels today.
I have eaten 5 bagels today.
I have eaten 6 bagels today.
I have eaten 7 bagels today.
Can also be written as
$i = 4; while ($i < 8) {
Print "I have eaten $i bagels today.\n";
$i + +;
}
You can use the control structure if and ElseIf:
if ($user _count > 200) {
Print "The site is busy right now!";
}
ElseIf ($user _count > 100) {
Print "The site is sort an active right now!";
else {
Print "The site is lonely-only $user _count user logged on.";
}
The rule of thumb for using operators can also be applied to control structures. You can also use switch,do...while, or even?: structure.