Beginners PHP Starter

Source: Internet
Author: User
Tags define expression function definition functions hash variables php code split
Beginner 2.PHP Beginners
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. Note the difference is that the string quoted by the quotation marks is literally defined, and the strings drawn from the double quotes can be extended. The backslash (\) can be used to split some special 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 detailed 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 + "Little piggies"; $foo is an integer (15)
$foo = 5 + "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

Logic and comparison

Logical operators are:
$a | | $b: Or
$a or $b: or
$a && $b: With
$a and $b: With
$a xor $b: (True when $a or $b is true, false when both)
! $a: Non-
Comparison operators are:
$a = = $b: equal
$a!= $b: Unequal
$a < $b: less than
$a <= $b: less Than or equal
$a > $b: Greater Than
$a >= $b: greater Than or equal
As with C, PHP also has a triple operator (?:). The bitwise operator is also present in PHP.

Priority

Just like C and Java!

2.7 Control Flow structure

PHP has the same process control as C. I'll probably introduce you in the following.

If, else, ElseIf, if (): endif

if (expression one)
{
. . .
}
ElseIf (Expression II)
{
. . .
}
Else
{
. . .
}
or like Python.
if (expression one):
. . .
. . .
ElseIf (Expression II):
. . .
else:
. . .
endif;

Loops. While, do .... While the For

while (expression)
{
. . .
}
Todo
{
. . .
}
while (expression);
for (expression one; expression two; expression III)
{
. . .
}
or like Python.
while (expr):
. . .
Endwhile;

Switch

Switch is the best replacement for multiple If-elseif-else structures:
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 the next loop.

Require, include

Just like the #include preprocessing in C. The file you specify in require replaces its location in the main file. You can use include () when you have a conditional reference file. This allows you to split complex PHP files into multiple files and refer to them separately as needed.

2.8 function

You can define your own functions like the following example. The return value of a 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 the function definition, even the definition of other functions and classes. The function must be defined before the reference.

2.9 Class

Use class models to build classes. You can refer to the PHP manual for a detailed explanation of the class.
Class Employee
{
var $empno; Number of employees
var $empnm; Employee Name

function Add_employee ($in_num, $in_name)
{
$this->empno = $in_num;
$THIS-&GT;EMPNM = $in_name;
}

Function Show ()
{
echo "$this->empno, $THIS-&GT;EMPNM";
Return
}

function Changenm ($in_name)
{
$THIS-&GT;EMPNM = $in_name;
}
}

$sbabu = new Employee;
$sbabu->add_employee ("Sbabu");
$sbabu->changenm ("Babu");

$sbabu->show ();

Translated from PHP Chinese users

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.