PHP Basics Tutorial (Basic PHP Primer Tutorial) Some code codes _php tutorial

Source: Internet
Author: User
Tags define function mysql download php basics php form php form processing php operator alphanumeric characters what php
Before this tutorial, I will not long to say what PHP is used. about what is a variable ah what is a judgment statement ah what, please check the relevant information by yourself this tutorial value is for people who have programming basics and are unfamiliar with PHP. The article is relatively simple. Mainly look at the structure. We also ask you to do a lot of research in detail.
PHP Environment Installation:
PHP is typically composed of: Mysql+php+apche also has iis+php+mysql or SQL Server
We can, of course, choose a combo package to install. The Novice recommends to install Appserv or Phpnow and so on.
IIS can use this installation to run a little bit of PHP support, MySQL needs to install.
You can also install individual parts of your own. Then configure it yourself.
Download address for each version of PHP:
Apche Download Address:
MySQL Download address:
Configuration Installation Tutorial:
Authoring tools: Suggest using notepad++ or Dreamweaver cs4
PHP's syntax is simple--look directly at the code: This is how the PHP code is declared. Note: This can be written as well, but it is not recommended.
The end of the marked statement: The semicolon is the end of the marked statement ";"--use ";" at the end of each statement. The semicolon indicates the end.
Comments in PHP:--See Code in the tutorial
Comments in PHP have a single-line comment://This is a comment
and large module Comments:/* This is a comment */
PHP variables are loose. But it's also case-sensitive, which you should pay attention to. Before using it, there is no need to declare-depending on how the variable is declared, PHP automatically converts the variable to the correct data type.
Declaring a variable in PHP is declared using the $ keyword--all variables are identified by $
Variable naming rules:
The variable name must begin with a letter or underscore "_".
Variable names can contain only alphanumeric characters and underscores.
Variable names cannot contain spaces. If the variable name consists of multiple words, it should be delimited with an underscore (such as $my _string), or start with a capital letter (such as $myString).
Note: (Basically all programming languages have the same variable naming rules!) )

Copy CodeThe code is as follows:
declaring variables
$var _name = "Snow";
Using variables
echo $var _name;
Displaying results: Snow

Declaration of constants in PHP:
Declaring constants in PHP is declared using the Define function. Look directly at code.
Copy CodeThe code is as follows:
The Define function has three parameters
First parameter: Specify the constant name--no keyword is used, the constant cannot have the $ symbol
Second parameter: Specify the value of the constant--only Boolean, Integer, floating point, string four types
Third parameter: Specifies whether this constant is case-sensitive--true ignores case, false is case-sensitive
Define ("Name", "Zhang San", true);
echo name;
/* Show results: Zhang San--because it is true, it is not case-sensitive */

There are predefined constants in PHP--you can query PHP manuals or related Materials
Arrays:--php arrays are still relatively simple and useful.
The PHP array can be used as a collection in other languages
PHP arrays can contain any type of PHP support. Of course, you can also store class objects, etc.--read code directly
Copy CodeThe code is as follows:
Array of values
$nums = Array (+/-);
or equivalent to
$nums [0] = 1;
$nums [1] = 2;
$nums [2] = 4;
Echo $nums [2]. "
/* Output: 4*/
Associative arrays-where the "= =" is an associative symbol in PHP, is the specified key-value pair.
$ns = Array ("name" = "Zhang San", "Age" =>22, "sex" and "man");
or equivalent to
$ns ["name"] = "Zhang San";
$ns ["age"] = 22;
$ns ["sex"] = "man";
echo "Name:". $ns ["name"]. "
Age: ". $ns [" Ages "]."
Gender: ". $ns [" Sex "]."
/* Output:
Name: Zhang San
Age: 22
Gender: Man
Multidimensional arrays--arrays can also be stored in arrays
$bs = Array ("Zhang San" =>array ("hobby" = "Computer", "age" = "23", "gender" = "male"), "Little Red" =>array ("hobby" = "eat", "sex" = "female") );
Adjust the format so that you can see clearly
$bs = array
"Zhang San" =>array
"Hobby" = "Computer",
"Age" = "23",
"Sex" = "male"
"Little Red" =>array
"Hobby" = "eat",
"Gender" and "Woman"
or equivalent to
$bs ["Little Red"] ["gender"] = 2; $bs ["Xiao Hong"] ["hobby"] = 2; //....
$bs ["Zhang San"] = Array ("hobby" = "Computer", "age" = "23", "gender" = "male"); $bs ["Little red"] = Array ("hobby" = "eat", "sex" = "female");
echo $bs ["Little Red"] [gender]. "
/* Output: Female */

PHP Operator:--Excerpt from W3school tutorial
This section lists the various operators used in PHP:
Arithmetic operators
operator Description Example Results
+ Addition x=2
- Subtraction x=2
* Multiplication X=4
/ Division 15/5
% Modulus (Division remainder) 5%2
++ Increment X=5
X + +
-- Decrement X=5
Assignment operators
operator Description Example
= X=y X=y
+= X+=y X=x+y
-= X-=y X=x-y
*= X*=y X=x*y
/= X/=y x=x/y
.= X.=y X=x.y
%= X%=y X=x%y

Comparison operators

operator Description Example
== is equal to 5==8 returns False
!= is not equal 5!=8 returns True
> is greater than 5>8 returns False
< is less than 5<8 returns True
>= is greater than or equal to 5>=8 returns False
<= is less than or equal to 5<=8 returns True

logical operators

operator Description Example
&& and X=6

(x < && y > 1) returns True

|| Or X=6

(x==5 | | y==5) returns false

! Not X=6

! (x==y) returns True

Program Judgment statement:

and C #, Java, C and other judgment statements. Have if: Else/else. If, switch statement--look directly at code
Copy the Code code as follows:
$name = "Zhang San"; declaring variables
/*if. Else only the statement executes one, and one condition is true. Even if the back of the establishment, will be ignored */
Determine if the name is Zhang San
if ($name = = "Zhang San")
echo "Zhang San";
else if ($name = = "John Doe")//Then judge
echo "John Doe";
else//None of the above go into else
echo "Other";
Print ('
'); Print output
$num = 1;
The switch selection structure can be similar in principle. Just add a break in the case--of course, not to add.
In this case, after playing case 1, it does not jump out, but continues to execute the next case branch. I didn't jump out until I met a break. You can try it on your own.
Switch ($num)
Case 1:
echo "one";
Case 2:
echo "Two";
Default://Defaults branch. Execute when the conditions are not set.
echo "Other";

The result of the final execution is:

PHP Loop:

As with other strongly typed programming languages. PHP also has while, do while, for, foreach--See Code directly

copy code code is as follows:
!--? php $index = 1;
while ($index <=10)
echo ". $index." Times "."
$index + +;//Cumulative
/* Results Output 10 times */

Echo '
$index = 1;
echo "". $index. " Times "."
$index + +;
while ($index <=1);

/* Results Output 1 times */
Echo '
for ($index = 1; $index <=3; $index + +)
echo ". $index." Times "."

/* Above results Output 3 times */
Echo '
$index = Array ("1", "2", "3"),
foreach ($index as $temp)//traversal array
echo "Value:". $temp. "
/* Above results Output 3 times */

PHP Functions:

The declaration of a PHP function is simple, as long as it is preceded by the function name followed by the keyword functions. --specific format directly read code
Copy the Code code as follows:
/*php function */
No parameter function
function Myecho ()
echo "No parameter function

Argument function--the passed in parameter can also be a class object
function MyEcho2 ($STR)
Echo $str;

Myecho (); Output: no parameter function
MyEcho2 ("Joking! "); Output: laughing and joking!

PHP class:

PHP supports object-oriented programming as well as other high-level languages. Here I say the basic part of the PHP class declaration. With regard to object-oriented programming, we study

The way PHP declares classes, you also add the keyword class--Specifically, code--which includes static functions. function calls, etc.)

Copy the Code code as follows:
Class MyClass//Declarations of classes
Private $jum 1; Defining private variables
Private $jum 2;
static public $test = "Test static method"; Defining Public variables
function Calc ()//class functions
return $this->jum1+ $this->jum2; The "-a" symbol is the meaning of a class call

function Setnum ($Num 1, $Num 2)//with parameter functions
$this->jum1 = $Num 1;
$this->jum2 = $Num 2;
return $this; Here to return the class object itself

static function Tt ()
echo "
". MyClass:: $test. "

/* Implement calculation function */
$temp = new MyClass;
echo $temp->setnum (2,8)->calc (); Output: 10
Myclass::tt (); "::" static call//output: Test static method

PHP form Processing:

When a page user submits a value, read the submitted value with $_get and $_post or $_request (which contains $_get, $_post, and $_cookie) system-defined variables-see code

Copy the Code code as follows:

echo $_post["XX"]. "
"; Read Post value
echo $_request["XX"];
Read the value with Get. Try it Yourself

So much for the time being ... If there is time, I will write down the common application of PHP. Advanced section. (including sessions, cookies, object-oriented, common functions, etc.) true techarticle before this tutorial, I will not long to say what PHP is used. about what is a variable ah what is a judgment statement ah what, please check the relevant information by yourself this tutorial value is for ...

  • Related Article

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