PHP Getting Started _php tutorial

Source: Internet
Author: User
Tags bitwise operators define function php programming
2.PHP Getting Started
The online tutorial for the PHP site has been great. There are also links to other tutorials there. This section of this article will let you familiarize yourself with PHP. I could not have done without any omission, and my purpose was only to allow you to quickly start your PHP programming.

2.1 First conditions

You first have to have a working Web server that supports PHP. I assume that all PHP files on your server have a. php3 extension.

2.2 Installation of PHP

Generate a file named Test.php3 that contains the following:

Then open the file in your browser. Look at this page and you'll know the options for your PHP installation.

2.3 Syntax

As mentioned earlier, you can mix and write 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 choose one of the things that you are most comfortable with and stick to it this way!

Detach from HTML

Here are the methods you can use:



<% ...%>

Statement

As with Perl and C, the statements are delimited in PHP (;). Flags that are separated from 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 line Comment
# UNIX Style single line comment

hello,world!

With the knowledge that we have learned, you can write a simplest program output that is perhaps the most famous word in the program world:


<title><br><?<br>echo "Hello world!"; <br>?><br></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. The variable type is usually not determined by the programmer and is determined by the PHP run process (really good relief!). )。 However, 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 use the following statement to assign a value to a number:
$a = 1234; # decimal Number
$a =-123; # negative
$a = 0123; # octal number (equal to 83 of decimal number)
$a = 0x12; # hexadecimal number (equal to 18 of decimal number)
$a = 1.234; # floating-point "double-precision number"
$a = 1.2e3; # The exponential form of double-precision numbers

String

A string can be defined by a field that is enclosed by single or double quotation marks. Note that the strings quoted in the enclosed quotation marks are literally defined, and the strings drawn from the double quotation marks can be expanded. A backslash (\) can be used to split some special characters. Examples are as follows:
$first = ' Hello ';
$second = "World";
$full 1 = "$first $second"; # Generate Hello World
$full 2 = ' $first $second '; # generates $first $second

You can connect characters and numbers with operational symbols. Characters are converted into numbers, using their original positions. 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 it depends on how you define them. You can use list () or array () to define them, or you can assign values directly to an array. 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] = "First";
$a [1] = "Second";
$a [] = "Third"; An easy 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 to teach long, let's add an element
$myphonebook ["Dean"] = "5397";
The Carale element you defined is wrong, let's correct it.
$myphonebook ["carole"] = "4522"
Did I not tell you how to use similar support for arrays? Let's take a look at
echo "$myphonebook [0]"; Sbabu
echo "$myphonebook [1]"; 5348

Some other functions that are useful for arrays or hash tables 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 (and does not require) defining variable types directly when declaring variables, and variable types will be determined by the circumstances in which they are applied." If you assign a value to 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

You may have noticed that the variable has a dollar sign ($) prefix. All variables are local variables, and the global statement is used in order to enable the use of external variables in the defined function. And you want to limit the scope of the variable to that function, using the static statement.
$g _var = 1; Global scope
function test ()
{
Global $g _var; This makes it possible to declare global variables.
}

More advanced is the variable representation of the variable. Please refer to the PHP manual. This can sometimes seem useful.

PHP has a number of 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 the commonly seen operators in c,c++ and Java. The precedence of these operators is also consistent. The assignment also uses "=".

Arithmetic and character

Only one of the following operators is related to characters:
$a + $b: Plus
$a-$b: minus
$a * $b: Multiply
$a/$b: except
$a% $b: modulo (remainder)
$a. $b: String Connection

Logic and comparison

The logical operators are:
$a | | $b: Or
$a or $b: or
$a && $b: With
$a and $b: With
$a XOR $b: XOR (True if $ A or $b is true, False if both)
! $a: Non-
Comparison operators are:
$a = = $b: equal
$a! = $b: Unequal
$a < $b: less than
$a <= $b: less than or equal to
$a > $b: Greater Than
$a >= $b: greater than or equal to
As with C, PHP has a triple operator (?:). Bitwise operators also exist in PHP.

Priority

Just like C and Java!

2.7 Control process Structure

PHP has the same process control as C. I will introduce you in the following.

If, else, ElseIf, if (): endif

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

Loops. While, do. While, for

while (expression)
{
. . .
}
Do
{
. . .
}
while (expression);
for (expression one; expression two; expression three)
{
. . .
}
Or just 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.
The continue is used to jump out of the remaining current loop and continue with the next loop.

Require, include

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

2.8 Functions

You can define your own functions as in the example below. 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.

Class 2.9

Use the class model to build the class. 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->empnm";
Return
}

function Changenm ($in _name)
{
$this-&GT;EMPNM = $in _name;
}
}

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

$sbabu->show ();

http://www.bkjia.com/PHPjc/316828.html www.bkjia.com true http://www.bkjia.com/PHPjc/316828.html techarticle the online tutorials for the 2.PHP Getting started PHP site are already great. There are also links to other tutorials there. This section of this article will let you familiarize yourself with PHP. I couldn't have done nothing .

  • 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.