Home php教程 php手册 PHP basic tutorial (php basic tutorial) some code code_php basics

PHP basic tutorial (php basic tutorial) some code code_php basics

May 16, 2016 am 09:00 AM
Getting started with php

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 do more research on your own for details
PHP environment installation:
The usual combination of PHP is: MySql PHP Apche also has IIS PHP MySQL or SqlServer
Of course we can choose the combination package for installation. Newbies are advised to install AppServ or phpnow, etc.
Under iis, you can use this to install and run it to support php. Mysql needs to be installed.
You can also install each part yourself. Then configure it yourself.
Download addresses of various versions of PHP: 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 /article/33062.htm
Writing tools: Notepad or dreamweaver cs4 is recommended ================================================ ====================
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, 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 according to the way the variable is declared.
Declaring variables in PHP uses the $ keyword to declare - 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:

Copy code The code is as follows:

//Declare variables
/ Declare constants using the define function. Look directly at code
Copy code The code is as follows:

/*
The define function has three 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--it can only be Boolean, Four types: integer, floating point, and string
The third parameter: Specify whether this constant is case-sensitive--true ignores case, false case-sensitive
*/
define("Name" ,"Zhang San",true);
echo name;
/*Display result: Zhang San--because it is true, it is not case sensitive*/
?>
There are also predefined constants in PHP - you can check the PHP manual or related information
============================== ========================================
Array: --PHP array Still 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. - just look at code
Copy code The code is as follows:

/*========================== =========================================*/
//Numeric array
          $nums = array(1,2,3);
                 // Or equivalent to  
           $nums[0] = 1; [2] = 4;
echo $nums[2]."
";
/*Output: 4*/
/*============== ================================================== ====*/
//Associative array --The "=>" is the association symbol in PHP, which specifies key-value pairs.
           $ns = array("name"=>"张三","age"=>22,"sex"=>"man");                                                           "] = "Zhang San";
                           $ns["age"]                                                                                                                                                                                                              ​"]."
Age:".$ns["age"]."
Sex:".$ns["sex"]."
";
/*Output:
Name: Zhang San
Age: 22
Gender: man
*/
/*======================== ============================================*/
/ /Multidimensional array--Arrays can also be stored in the array
$bs = array("张三"=>array("Hobby"=>"Computer","Age"=>"23","Gender"=>" Male"),"Xiaohong"=>array("Hobby"=>"Eating","Gender"=>"Female"));
//Adjust the format so that everyone can see it more clearly
bs = array
        (
                                                                                                                                                                                                                                                                                        
"Gender" =>"male"
 " >"女"
)
       );
          // Or equivalent to ....
                                                                                                                                                                                                                                                $bs ["小红"] = array("Hobby"=>"Eating","Gender"=>"Female");
        echo $bs["小红"]["Gender"]."
";
          /*Output: Female*/
                                                                                                   ==================================*/
?>

================================================ ======================
PHP operators: --Excerpt from w3school's tutorial

This section lists the operators used in PHP Various operators:
Arithmetic operators
Operator Description Example Results
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
Assignment operator
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
>= is greater than or equal to 5>=8 returns false
is less than or equal to 5

Logical operators

Operator Description Example
&& and x=6
y=3

(x 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

Copy code The code is as follows:

$name = "Zhang San"; //Declare variables
/*if..else will only execute statements One, one condition is established. Even if the following is true, it will be ignored*/
                                                                                                                                                                                                        Determine whether the name is Zhang San.
}
Else if ($ name == "Li Si") // Then judge
{
echo "Li Si";
}
else // or above is not just 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)   
                                                                                                                                                  2:
echo "二";
              break; Executed when none of the conditions are true.
             echo "other";一
                                                                               >

PHP loop:

It is the same as other strongly typed programming languages. PHP also has while, do while, for, and foreach -- look at the code directly

Copy code The code is as follows:

$index = 1;
while($index {
echo "th". $index."times"."
";
                                                                                                $index. ;
         $index = 1; > while($index
/*The above results are output once*/
echo '
';
for($index = 1;$index echo '
';
$index = array("1","2","3");
foreach($index as $temp) //Traverse the array
{
echo "Value:".$temp."
";
}
/*The above results are output 3 times*/
?>

PHP function:

The declaration of a PHP function is very simple. Just add the keyword function followed by the function name. --Please see code

Copy code The code is as follows:

/*PHP function*/
Echo "Portomless function
}
// There are participation functions-the passing parameters can also be class objects. BR>            echo $str;
                                                                                                                                                                                                                                                                                    ;
?>

PHP class:

PHP, like other high-level languages, supports object-oriented programming. Here I talk about the declaration of the basic part of the php class. Regarding object-oriented programming, you can do your own research

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
jum2;
          static public $test = "Test static method"; //Define public variables
           function Calc() //                                                                                                                                                                            . / "->" symbol means class call
                                                                                                                                                          um1 ;
$ this->JUM2 = $ num2;
Return $ this; // Here the class object itself
}

Static function tt ()
{
echo echo "
".MyClass::$test."
";                   > 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:


echo $_POST["xx"]."
"; //Read post value
echo $_REQUEST["xx"];
//Use get to read the value. Try it yourself
?>






That's all for now... If I have time, I will write down the commonly used applications of PHP. Advanced section. (Including sessions, cookies, object-oriented, common functions, etc.)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Beginner's Guide to PHP: TCP/IP Programming Beginner's Guide to PHP: TCP/IP Programming May 20, 2023 pm 09:31 PM

As a popular server-side scripting language, PHP can be used not only for the development of Web applications, but also for TCP/IP programming and network programming. In this article, we will introduce you to the basics of TCP/IP programming and how to use PHP for TCP/IP programming. 1. Basic knowledge of TCP/IP programming TCP/IP protocol is the standard protocol for communication on the Internet. It is composed of two parts: TCP protocol and IP protocol. The TCP protocol is responsible for establishing reliable connections

Getting Started with PHP: Basic PHP Syntax Getting Started with PHP: Basic PHP Syntax May 20, 2023 am 08:39 AM

PHP is a server-side scripting language that is used to develop dynamic websites, web applications, and web programs. PHP has a wide range of applications, and both beginners and experienced developers can benefit from it. This article will provide you with an introductory guide to the basic syntax of PHP. If you want to learn PHP programming and build a solid foundation from scratch, you've come to the right place. The basic structure of PHP. A PHP program contains the following three parts: <?php//PHP code?>& on both sides of the code

Getting Started with PHP: File Uploading and Downloading Getting Started with PHP: File Uploading and Downloading May 22, 2023 am 10:51 AM

In web development, file uploading and downloading is a very common requirement. Whether users upload avatars or documents, or administrators ask users to download a file, this function is needed. As a powerful server-side language, PHP naturally provides powerful file operation functions and class libraries, allowing us to easily implement file upload and download functions. This article will introduce the basic process and common functions to implement file upload and download in PHP, and provide sample code. If you are a PHP beginner or are learning file operations

A Beginner's Guide to PHP A Beginner's Guide to PHP May 25, 2023 am 08:03 AM

PHP is a popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

Getting Started with PHP: JSON Extension Getting Started with PHP: JSON Extension May 20, 2023 am 08:37 AM

PHP is a widely used programming language, especially in web development, PHP occupies an important position. Among them, JSON is a common data format that can be used to store and transmit data. JSON extensions are provided in PHP to facilitate developers to operate and process JSON data. This article will introduce the basic usage and application scenarios of JSON extension. 1. Basic usage of JSON extension. Convert JSON string into PHP object or array. The json_decode() function in PHP can convert

Getting Started with PHP: Some Common HTTP Status Codes Getting Started with PHP: Some Common HTTP Status Codes May 21, 2023 am 08:15 AM

For PHP beginners, it is very important to understand HTTP status codes. HTTP status code refers to the 3-digit code returned by the web server and is used to indicate the processing result of the client request. This article will introduce some common HTTP status codes and their meanings to help PHP beginners better understand the various HTTP status codes encountered during website development. 200OK200OK is one of the most common HTTP status codes, indicating that the request was successfully processed and a result was returned. When you visit a website, such as

Getting Started with PHP: Cache Settings Getting Started with PHP: Cache Settings May 20, 2023 am 08:10 AM

PHP is a very popular programming language that is often used in the field of Internet development. In PHP development, cache settings are a very important part. Caching can improve website performance and user experience, reduce server load, and is one of the common methods for website optimization. This article will introduce you to the introductory guide to setting up PHP cache. 1. What is cache? Caching is to store some frequently accessed data in memory so that it can be quickly obtained the next time it is accessed, avoiding repeated calculations or database queries and improving response speed. PHP, slow

Getting Started with PHP: Code Version Management Getting Started with PHP: Code Version Management May 24, 2023 am 08:13 AM

In software development, version management is an extremely important link. Because writing code in a team inevitably requires merging everyone's code. Version management tools can help us track code changes and avoid conflicts when merging. Among them, Git is currently the most popular version management tool, a must-have for both personal development and team collaboration. This article will focus on Git to introduce you to the benefits of using version management tools, the basic concepts and basic operations of Git, and explain how to use Git to collaborate with your team for development. Why do we need versions

See all articles