2. Getting Started with PHP
The online tutorials on the PHP site are already great. There are also links to some other tutorials there. This part of the article will make you familiar with PHP. It is impossible for me to miss anything, my purpose is only to allow you to quickly start your PHP programming.
2.1 First Conditions
You must first have a working web server that supports PHP. I assume that all PHP files on your server have the extension .php3.
2.2 PHP installation
Generate a file named test.php3 with the following content:
Then in your Open this file in your browser. Take a look at this page to see what options your PHP installation uses.
2.3 Syntax
As mentioned before, you can mix your PHP code and HTML code. So you have to have a way to differentiate between the two. Here are a few ways you can do it. You can pick whichever one you're most comfortable with and stick with it!
Separate
from HTML Here are the methods you can use:
. . . ?>
<% . . . %>
Statement
The same as Perl and C, Use (;) to separate statements in PHP. Those separate tags from HTML also indicate the end of a statement.
Comments
PHP supports C, C++ and Unix style comments:
/* C, C++ style multi-line comments*/
// C++ style single line Comments
# Unix-style single-line comments
Hello, World!
With the knowledge we have learned, you can write the simplest program to output one of perhaps the most famous names in the programming world words:
echo "Hello World!";
?>
< ;/TITLE>
First PHP page
// Single line C++ style comment
/*
printing the message
*/
echo "Hello World!";
# Unix style single line comment
?>
2.4 Data Types
PHP supports integers, floating point numbers, strings, arrays and objects. Variable types are usually not determined by the programmer but by the PHP runtime (what a relief!). But the type can also be set explicitly by the function cast or settype().
Numeric value
The numeric value type can be an integer or a floating point number. You can use the following statements to assign a value:
$a = 1234; # Decimal number
$a = -123; # Negative number
$a = 0123; # Octal number (equal to decimal number 83)
$a = 0x12; # Hexadecimal number (equal to 18 decimal numbers)
$a = 1.234; # Floating point number "double precision number"
$a = 1.2e3; # Double Exponential form of precision number
String
Strings can be defined by fields enclosed in single or double quotes. Note that the difference is that strings enclosed in single quotes are defined literally, while strings enclosed in double quotes can be expanded. Backslash () can be used to separate certain special characters. For example:
$first = 'Hello';
$second = "World";
$full1 = "$first $second"; # Generate Hello World
$full2 = '$first $ second';# produces $first $second
can connect characters and numbers using arithmetic symbols. Characters are converted to numbers using their original position. There are detailed examples in the PHP manual.
Arrays and Hash Tables
Arrays and hash tables are supported in the same way. How you use them depends on how you define them. You can define them using list() or array(), or assign values to arrays directly. The index of the array starts from 0. Although I haven't explained it here, you can easily use multidimensional arrays.
//An array containing two elements
$a[0] = "first";
$a[1] = "second";
$a[] = " third"; // Simple way to add array elements
// Now $a[2] is assigned the value "third"
echo count($a); // Print out 3 because the array has 3 elements
//Use one statement to define an array and assign a value
$myphonebook = array (
"sbabu" => "5348",
"keith" => "4829",
"carole" => "4533"
);
// Oh, forget about the dean, let's add an element
$myphonebook["dean"] = "5397";
// The carale element you defined is wrong, let’s correct it
$myphonebook["carole"] => "4522"
// Haven’t I told you how to use similar support for arrays? ? Let's take a look at
echo "$myphonebook[0]"; // sbabu
echo "$myphonebook[1]"; // 5348
Some others useful for arrays or hash tables The functions include sort(), next(), prev() and each().
Object
Use the new statement to generate an object:
class foo
{
function do_foo ()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
Change variable type
Mentioned in PHP manual : "PHP does not support (and does not require) defining the variable type directly when declaring the variable; the variable type will be determined based on the situation in which it is used. If you assign the variable var to a string, then 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 ( 3.3)
$foo = 5 + "10 Little Piggies"; // $foo is an integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
If you want to forcefully convert the variable type, you can use the same function settype() as in C language.
2.5 Variables and Constants
You may have noticed that variables are prefixed with a dollar sign ($). All variables are local variables. In order to use external variables in the defined function, use the global statement. And if you want to limit the scope of the variable to the function, use the static statement.
$g_var = 1; // Global scope
function test()
{
global $g_var; // This way global variables can be declared
}
More More advanced is the variable representation of variables. Please refer to the PHP manual. This can sometimes be useful.
PHP has many built-in defined variables. You can also use the define function to define your own constants, such as define("CONSTANT", "value").
2.6 Operators
PHP has the commonly seen operators in C, C++ and Java. The precedence of these operators is also consistent. Assignment also uses "=".
Arithmetic and characters
Only one of the following operators is character related:
$a + $b: add
$a - $b: subtract
$ a * $b: Multiply
$a / $b: Divide
$a % $b: Modulo (remainder)
$a. $b: String concatenation
logical sum Comparing
logical operators are:
$a || $b: or
$a or $b: or
$a && $b: with
$a and $ b: with
$a xor $b: exclusive or (true when $a or $b is true, false when both are the same)
! $a: non-
comparison operators are:
$a == $b: equal
$a != $b: not equal
$a < $b: less than
$a <= $b: less than or equal to
$a > $b : Greater than
$a >= $b : Greater than or equal to
Like C, PHP also has a triple operator (?:). Bit operators also exist in PHP.
Priority
Just like C and Java!
2.7 Control flow structure
PHP has the same flow control as C. I will briefly introduce it below.
if, else, elseif, if(): endif
if (expression one)
{
. . .
}
elseif (expression 2)
{
. . .
}
else
{
. . .
}
// or like Python
if (expression 1) :
. . .
. . .
elseif (Expression 2) :
. . .
else :
. . .
endif ;
Loops. while, do..while, for
while (expression)
{
. . .
}
do
{
. . .
}
while (expression);
for (expression one; expression two; expression three)
{
. . .
}
/ / Or like Python
while (expr) :
. . .
endwhile ;
switch
switch is the best for multiple if-elseif-else structures Replacement:
switch ($i)
{
case 0:
print "i equals 0";
case 1:
print "i equals 1";
case 2:
print "i equals 2";
}
break, continue
break interrupts the current loop control structure.
continue is used to jump out of the remaining current loop and continue executing the next loop.
require, include
just like #include preprocessing in C. The file you specify in require will replace its location in the main file. When referencing a file conditionally, you can use include(). This allows you to split complex PHP files into multiple files and reference them separately when needed.
2.8 Functions
You can define your own functions like the following example. The return value of the function can be any data type:
function foo (variable name one, variable name two, . . . , variable name n)
{
echo "Example function.n";
return $retval;
}
All PHP code can appear in function definitions, even definitions of other functions and classes. Functions must be defined before being referenced.
2.9 Classes
Use class models to create classes. You can refer to the detailed explanation of classes in the PHP manual.
class Employee
{
var $empno; // Number of employees
var $empnm; // Employee name
function add_employee($in_num, $in_name)
{
$this->empno = $in_num;
$this->empnm = $in_name;
}
function show()
{
echo "$ this->empno, $this->empnm";
return;
}
function changenm($in_name)
{
$this->empnm = $ in_name;
}
}
$sbabu = new Employee;
$sbabu->add_employee(10,"sbabu");
$sbabu->changenm(" babu");
$sbabu->show();
http://www.bkjia.com/PHPjc/316828.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316828.htmlTechArticle2. Getting Started with PHP The online tutorials for PHP sites are already great. There are also links to some other tutorials there. This part of the article will make you familiar with PHP. I can't do it without leaving anything behind...