


PHP basic tutorial (php basic tutorial) some code_PHP tutorial
Before this tutorial, I will not talk about the common uses of PHP in a long and uniform way. Regarding what is a variable, what is a judgment statement, etc., please check the relevant information by yourself. This tutorial is intended for people who have a programming foundation and are new to PHP. The article is relatively simple. Mainly depends on the structure. Please study it yourself for details
PHP environment installation:
The usual combination of PHP is: MySql+PHP+Apche, there is also IIS+PHP+MySQL or SqlServer
Of course we can choose the combination package for installation. Newbies are advised to install AppServ or phpnow, etc.
You can use this installation and running under iis to support php. Mysql needs to be installed.
You can also install each part yourself. Then configure it yourself.
PHP download address: http://museum.php.net/php5/
Apche download address: http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe ?download
MySQL download address: http://www.mysql.cn/
Configuration and installation tutorial: http://wenku.baidu.com/view/c6118b1810a6f524ccbf85f9.html
Or http://www .jb51.net/article/33062.htm
Writing tools: It is recommended to use Notepad++ or dreamweaver cs4
========================== ===========================================
Syntax:
The syntax of PHP is very simple - just look at the code: This is how PHP code is declared. Note: ?> can also be written like this, but it is not recommended.
Mark the end of a statement: The semicolon marks the end of a statement ";" -- a ";" semicolon should be used after each statement to indicate the end.
============ ================================================== ========
Comments in PHP: --See the code in the tutorial
for details. Comments in PHP have single-line comments: //This is the comment
and the large module comment: /* This is a comment*/
============================================ ===========================
Variables:
PHP variables are loose. But it is also case-sensitive, so everyone should pay attention to this. There is no need to declare it before using it - PHP will automatically convert the variable into the correct data type based on the way the variable is declared.
Declaring variables in PHP is declared using the $ keyword - all variables are identified by $
Variable naming rules:
Variable names must start with a letter or underscore "_".
Variable names can only contain alphanumeric characters and underscores.
Variable names cannot contain spaces. If the variable name consists of multiple words, they should be separated by underscores (such as $my_string) or start with a capital letter (such as $myString).
Note: (Basically all programming languages have similar variable naming rules!)
Example:
? & GT;
Constant:
Police in PHP:
State the define function in PHP to declare the constant using the define function to use the define function declared. Directly look at the code
Copy the code
The code is as follows:
/* There are three define functions Parameters The first parameter: Specify the constant name--keywords are not allowed, and the constant cannot have the $ symbol The second parameter: Specify the value of the constant--can only be Boolean, integer, floating point, character String four types The third parameter: Specify whether this constant is case-sensitive - true ignores case, false case-sensitive
echo name;
/*Display result: Zhang San--because it is true, it is not case sensitive*/
?>
There are also presets in PHP Define constants--you can check the PHP manual or related information
==================================== ==================================
Array: --PHP’s array is relatively simple and easy to use .
PHP arrays can be used as collections in other languages
PHP arrays can store any type supported by PHP. Of course, you can also store class objects, etc. --Look directly at the code
Copy the code
The code is as follows:
/*==================================== ================================*/
//Numerical array
$nums = array(1 ,2,3);
echo $nums[2]."
";
/*Output: 4*/
/*=================== ================================================*/
//Associative array --The "=>" is the association symbol in PHP, which specifies key-value pairs.
$ns = array("name"=>"Zhang San","age"=>22,"sex"=>"man"); ns["name"] = "Zhang San";
$ns["age"] = 22;
$ns["sex"] = "man";
ns["name"] ns["name"]."
Age:".$ns["age"]."
Sex:".$ns["sex"]."
";
==== ================================================== =======*/
//Multi-dimensional array--arrays can also be stored in the array
,"age"=>"23","gender"=>"male"),"小红"=>array("hobby"=>"eating","gender"=>"female" ));
$bs = "Hobby" =>"Computer",
"Age"=>"23",
"Gender"=>"Male"
" ),
"Xiaohong"=>array
//Or equivalent to
$bs[ "Xiaohong"]["Gender"] = 2; $bs["Xiaohong"]["Hobby"] = 2; //....
"] = array("Hobby"=>"Computer","Age"=>"23","Gender"=>"Male"); $bs["小红"] = array("Hobby" = & gt; "Eat", "Gender" = & GT; "Female");
Echo $ bs ["Little Red"] [Gender "]." Output: Female*/
/*========================================== ===========================*/
?>
=====================================================================
PHP运算符: --摘录w3school的教程
本部分列出了在 PHP 中使用的各种运算符:
算数运算符
运算符 | 说明 | 例子 | 结果 |
---|---|---|---|
+ | Addition | x=2 x+2 |
4 |
- | Subtraction | x=2 5-x |
3 |
* | Multiplication | x=4 x*5 |
20 |
/ | Division | 15/5 5/2 |
3 2.5 |
% | Modulus (division remainder) | 5%2 10%8 10%2 |
1 2 0 |
++ | Increment | x=5 x++ |
x=6 |
-- | Decrement | x=5 x-- |
x=4 |
运算符 | 说明 | 例子 |
---|---|---|
= | 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 |
比较运算符
运算符 | 说明 | 例子 |
---|---|---|
== | 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 |
逻辑运算符
运算符 | 说明 | 例子 |
---|---|---|
&& | and | x=6 y=3 (x < 10 && y > 1) returns true |
|| | or | x=6 y=3 (x==5 || y==5) returns false |
! | not | x=6 y=3 !(x==y) returns true |
Program judgment statement:
It is the same as C#, java, C and other judgment statements. There are if..else/else..if and switch statements - look directly at Code
& lt;? PHP
$ name = "Zhang San"; // State the variable
/*If..lse that only one of the statements will be executed, one condition is established. Even if the later ones are established, it will be ignored*/
// determine whether the name is Zhang San
if ($ name == "Zhang San")
{
echo "Zhang San";
}
{
echo "Li Si"; Go into else
/ *
The principle of switch selection structure and if is similar. Just add break in the case -- of course you don't have to add it.
In this case, after executing case 1, it will not jump out, but continue to execute the next case branch. Don't jump out until you encounter a break. You can try it yourself
*/
switch($num)
{
case 1:
echo "一";
break;
case 2:
echo "二";
break;
default: //Default branch. Executed when none of the conditions are true.
echo "other"; 🎜>
PHP Loop:
Same as other strongly typed programming languages. PHP also has while, do while, for, and foreach -- just look at the code
Copy code
The code is as follows:
$ Index = 1;
While ($ Index & LT; = 10)
{
Echo ".
";
; 🎜> $index = 1; }
while($index <=1);
/*The above results are output once*/
echo '
';
for($index = 1; $index <=3;$index++)
The above result is output 3 times */
have been have been been been been been — been been been been been been. Temp) // Traversing the array
{
echo ":". $ Temp. "& LT; br /& gt;"
}
/*results output 3 times* /
?>
PHP function:
The declaration of a php function is very simple. Just add the keyword function in front and followed by the function name. --Please see the code directly for the specific format
Copy the code
The code is as follows:
/*PHP function / Parameter function - the passed-in parameter can also be a class object
function MyEcho2($str)
{
echo $str;
MyEcho(); //output :No parameter function
MyEcho2("Heheheha!"); //Output: Hehehaha!
?>
When declaring a class in PHP, you also need to add the keyword class - see code for details - (including static functions, function calls, etc.)
Copy code
The code is as follows:
class MyClass //Class declaration
$test = "Test static method"; //Define public variables
" symbol means class call
= $Num1;
🎜> {
"
".MyClass::$test."
"; = new MyClass;
echo $temp->SetNum(2,8)->Calc(); //Output: 10
MyClass::Tt(); //"::"static call/ /Output: Test static method
?>
PHP form processing:
When the page user submits the value, use $_GET and $_POST or $_REQUEST (which includes $_GET, $_POST and $_COOKIE) system-defined variables to read the submitted value -- see code
Copy code
The code is as follows:
"xx"]."
"; //Read the post value
echo $_REQUEST["xx"]; Try it yourself
?>
That’s all for now...if you have time , I will write down the commonly used applications of PHP. Advanced section. (Including sessions, cookies, object-oriented, common functions, etc.)
true
http: //www.bkjia.com/PHPjc/326390.html
TechArticle
Before this tutorial, I will not talk about the commonly used PHP in a long way. Regarding what is a variable, what is a judgment statement, etc., please check the relevant information by yourself. This tutorial is aimed at those who have...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
