2.PHP Getting Started
The online tutorials on the PHP site have been great. There are also links to some other tutorials. This section of this article will give you a bit of familiarity with PHP. I can not do without any omission, my purpose is to let you quickly start your PHP programming.
2.1 First-order conditions
You must first have a Web server that is working to support PHP. I assume that all PHP files on your server have an extension of. php3.
Installation of 2.2 PHP
Generate a file named Test.php3 that contains the following:
? Phpinfo ();?>
Then open the file in your browser. Take a look at this page and you will know the options you use for your PHP installation.
2.3 Grammar
As mentioned earlier, you can mix your PHP code and HTML code. So you have to have a way to distinguish between the two. Here are a few ways you can use it. You can choose one of the things that you are most comfortable with and stick to this way!
Detach from HTML
The following are the methods you can use:
? . . . ? >
<?php ...? >
<script language= "php" > ... </script>
<% ...%>
Statement
Like Perl and C, use (;) to separate statements in PHP. The flags that are separated from the HTML also represent the end of the statement.
Comments
PHP supports c,c and UNIX-style annotation methods:
/* C,c style multi-line Comment * *
C-style Single-line Comment
# UNIX Style Single-line Comment
hello,world!
With the knowledge that we've learned, you can write one of the simplest programs to output one of the most famous words in the program world:
<HTML>
<HEAD>
<TITLE>
?
echo "Hello world!";
?>
</TITLE>
</HEAD>
<BODY>
<H1>
The I-PHP page
</H1>
<HR>
?
Single line C style comment
/*
Printing the message
*/
echo "Hello world!";
# Unix Style single line comment
?>
</BODY>
</HTML>
2.4 Data types
PHP supports integers, floating-point numbers, strings, arrays, and objects. Variable types are usually not determined by the programmer and are determined by the PHP process (good riddance!). )。 But the type can also be explicitly set by the function cast or settype ().
Numerical
numeric types can be integers or floating-point numbers. You can assign a value to a number using the following statement:
$a = 1234; # decimal Number
$a =-123; # negative numbers
$a = 0123; # octal number (equal to 83 of decimal number)
$a = 0x12; # hexadecimal numbers (equal to 18 of decimal digits)
$a = 1.234; # floating-point number ' double precision '
$a = 1.2e3; # exponential form of double-precision numbers
String
A string can be defined by a field that is enclosed in single or double quotes. The difference is that the strings drawn by the quotes are literally defined, and the strings that are quoted by the double quotes can be extended. The backslash () can be used to segment some extraordinary characters. Examples are as follows:
$first = ' Hello ';
$second = "World";
$FULL1 = "$first $second"; # produces Hello world
$full2 = ' $first $second '; # produce $first $second
You can concatenate characters and numbers by using an operator symbol. The characters are converted into numbers, taking advantage of their initial position. There are specific examples in the PHP manual.
Arrays and Hash tables
Arrays are supported in the same way as hash tables. How you use them depends on how you define them. You can use list () or array () to define them, or you can assign values directly to arrays. The index of the array starts at 0. Although I do not explain here, but you can easily use a multidimensional array.
An array that contains two elements
$a[0] = "a";
$A[1] = "Second";
$a[] = "Third"; Simple way to add an array element
Now $A[2] is assigned the value "third"
echo count ($a); Print out 3 because the array has 3 elements
Define an array with a statement and assign a value
$myphonebook = Array (
"Sbabu" => "5348",
"Keith" => "4829",
"Carole" => "4533"
);
Oh, forget the godson, let's add an element
$myphonebook["Dean"] = "5397";
The Carale element you defined is wrong, let's correct it
$myphonebook["Carole"] => "4522"
Have I not told you how to use array of similar support methods? Let's take a look at
echo "$myphonebook[0]"; Sbabu
echo "$myphonebook[1]"; 5348
Other functions that are useful for an array or hash table include sort (), Next (), Prev (), and each ().
Object
Use the new statement to produce an object:
Class Foo
{
function Do_foo ()
{
echo "doing foo.";
}
}
$bar = new Foo;
$bar->do_foo ();
Change Variable type
As mentioned in the PHP manual: "PHP does not support (or need) to define variable types directly when declaring variables, and the type of the variable will be determined according to the circumstances in which it is applied. If you assign a variable var to a string, it becomes a string. If you assign an integer value to it, it becomes an integer. "
$foo = "0"; $foo is a string (ASCII 48)
$foo; $foo is the string "1" (ASCII 49)
$foo = 1; $foo is now an integer (2)
$foo = $foo 1.3; $foo is a double precision number (3.3)
$foo = 5 "ten Little piggies"; $foo is an integer (15)
$foo = 5 "ten Small Pigs"; $foo is an integer (15)
If you want to forcibly convert a variable type, you can use the same function Settype () as the C language.
2.5 Variables and constants
As you may have noticed, variables have a prefix of dollar sign ($). All variables are local variables, and the global statement is used to allow external variables to be used in the defined function. And you're going to limit the scope of the variable to that function, using the static statement.
$g_var = 1; Global scope
function test ()
{
Global $g_var; So you can declare global variables.
}
A bit more advanced is the variable representation of the variable. Please refer to the PHP manual. This can be very useful at times.
PHP has many defined variables built into it. You can also use the Define function to define your own constants, such as define ("CONSTANT", "value").
2.6 operator
PHP has an operator that is commonly seen in C,c and Java. The precedence of these operators is also consistent. Assignment also uses "=".
Arithmetic and character
Only one of the following operators is related to characters:
$a $b: Add
$a-$b: minus
$a * $b: Multiply
$a/$b: except
$a% $b: modulo (remainder)
$a. $b: string concatenation