Home Backend Development PHP Tutorial One of PHP introductory study notes_PHP tutorial

One of PHP introductory study notes_PHP tutorial

Jul 21, 2016 pm 03:33 PM
html php code getting Started Basic study Embed method of notes Script grammar

1. Basic syntax
The way to embed a PHP script in HTML code is to write the code in . Output data to the browser using the echo or print function. echo can accept multiple parameters, print can only accept one. The form of echo is

void echo(string arg1,[,...string argn]);

PHP syntax allows the omission of parentheses. For example,

Copy code The code is as follows:

$my =' my ';
echo 'Hello',$my,'world'
?>

will output Hello my world on the browser

PHP also supports a and C language A very similar printf function, such as printf(' %d apples',100), will output 100 apples. The usage of sprintf is the same as printf, but instead of outputting to the browser, it returns a string.

2. Data types and variables

PHP is weakly typed. A variable does not need to be declared in advance, nor does it need to specify a type. Variables in PHP are $ plus the variable name. PHP variables are case-sensitive. For example, $my='my' in the above example.

The variable types supported by PHP include: Boolean, integer, floating point, string, array and object. The first four are very commonly used and are similar to other languages, so I won’t introduce them in detail. Arrays and objects will be introduced in detail later.

There are functions in PHP to detect the type of objects, they are getttype. gettype returns a string whose value can be array, boolean, double, integer, object, resource, string and unknown type. PHP also supports explicit type conversion, and the syntax is similar to C. The

conversion operator converts to
(array) array
(bool) (boolean) boolean
(int) (integer) integer
(object) object
( float), (double), (real) floating point number
(string) string

For example:
Copy code The code is as follows :

$str ='a string';
$num=15;
$numstr='123.3';
echo gettype($ str),'
';
echo gettype($num),'
';
echo gettype($numstr),'
';
$numstr=(float)$numstr;
echo gettype($numstr);
?>

The output result is:

string
integer
string
double

There are also functions that can be used to determine whether a variable is of a certain type, such as is_array(), is_bool(), etc. The usage is similar.

3. Function and variable scope
The method of declaring functions in PHP is very simple, in the following form:
Copy code The code is as follows:

function functionName(parameters){

function body

}

No need to specify the return type , there is no need to specify the variable type in the brackets, as long as there is a variable name. For example:
Copy code The code is as follows:

function taxedPrice($price,$taxrate) {
return $price*(1+$taxrate);
}
echo taxedPrice(100, 0.03);
?>

By default, PHP Parameters are passed by value. Changing the value of the parameter within the function will not change the value of the variable outside the function. However, PHP also supports passing by reference. The syntax is consistent with C, &$paramName. For example, the following is a classic example:
Copy code The code is as follows:

function swap1($x,$y){
$t=$x;$x=$y;$y=$t;
}
function swap2(&$x,&$y){
$t=$x;$x= $y;$y=$t;
}
$a=3;$b=5;
swap1($a,$b);
printf("a is %d, b is %d
",$a,$b);
swap2($a,$b);
printf("a is %d, b is %d
?>

Output result:

a is 3, b is 5
a is 5, b is 3

PHP functions also support default values ​​for parameters, and the syntax is the same as C. For example:
Copy code The code is as follows:

function taxedPrice($price,$taxrate= 0.03){
return $price*(1+$taxrate);
}
echo taxedPrice(100);
?>

The scope of variables is introduced below. PHP's variable scope is very similar to C. There are four types: local variables, function parameters, global variables, and static variables. Local variables are variables declared within the function, and function parameters are variables declared at the beginning of the function; variables not declared in the function are global variables, and global variables can be accessed anywhere, but unlike C, if they are in a function To modify the value of a global variable, you need to explicitly specify it as a global variable using the GLOBAL keyword, otherwise PHP will declare a local variable with the same name and overwrite it. For example:
Copy code The code is as follows:

$taxrate=0.03; //global
function change1() {
$taxrate+=1;
}
function change2() {
GLOBAL $taxrate;
$taxrate+=1;
}
change1 ();
echo $taxrate,'
';
change2();
echo $taxrate,'
';
?>

The output result is:

0.03

1.03

PHP also has a super global variable. Super global variables are predefined by the PHP system and are mainly used to access environment-related information, such as the current user session, user operating environment and local environment. The super global variable is an array. For example, $_SERVER stores server-related information. $_GET, $_POST, $_FILES, and $_COOKIE respectively store information submitted by the client using get and post, uploaded files, cookie information, etc. The use of these variables is very simple. If you need any information, you only need to look up

4. The variables of variables
are different from static languages ​​such as C. The variable name of PHP itself can be a Variables, which are very convenient when many variables need to be generated dynamically. For example:
Copy code The code is as follows:

$r="hello";
$$r="I am hello";
echo $hello;
?>

The output result is: I am hello


5. Flow control statements
mainly include, if else, while, for, do while, switch. These are very similar to C language and are basically the same. Not much introduction. There are some differences. PHP's elseif is a keyword that is connected together, while C language is else if.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322534.htmlTechArticle1. Basic syntax The way to embed PHP scripts in HTML code is to write the code in ?php?. Output data to the browser using the echo or print function. echo can accept multiple parameters, print can only...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles