Introduction to PHP

Source: Internet
Author: User
Tags closing tag echo command php print php script variable scope alphanumeric characters what php

What is PHP?

PHP is the acronym for PHP Hypertext Preprocessor

PHP is a widely used open-source scripting language

PHP scripts are executed on the server

PHP has no cost and is free to download and use

What PHP can do

PHP can generate dynamic page content

PHP can create open read write delete and close file on server

PHP can receive form data

PHP can send and retrieve cookies

PHP can add delete to modify data in database

PHP can restrict access to certain pages in the site

PHP is capable of encrypting data

With PHP, you can export only HTML, you can also output images, PDF files

PHP syntax

The PHP script executes on the server and then sends back the HTML results to the browser.

Basic PHP syntax

PHP scripts can be placed anywhere in the Yvendong.

The PHP script has <?php to the beginning, ending with?>:

<?php

Here is the PHP code

?>

The default file name extension for php files is. php

PHP files usually contain HTML tags and some PHP script code.

The following example is a simple PHP file that contains the use of the built-in PHP function echo to output text on a Web page Hello World

NOTE: The PHP statement ends with a semicolon (;). The closing tag of the PHP block will also automatically be the table's symbol (so the last line of the PHP code does not have to

Use semicolons).

Comments in PHP

Comments in the PHP code are not read and executed as programs. His only role is for code editors to read.

Comments are used to:

1 Make others understand the work you're doing-comments can let other programmers know what you're doing at each step

2 Remind yourself of what you've done, most programmers have gone through a year or two to rework the project and then have to reconsider

The things they've done. Comments can record the way you think when you write code.

PHP supports three types of annotations:

1//Single line comment

2//Single line comment

3/**/Multi-line Comment

PHP Case Sensitive

In PHP, all user-defined functions, classes, and keywords (such as if else echo) are not case sensitive

But

Case sensitive in all variables

PHP variables

A variable is a container for storing information:

Variable naming rules:

The variable starts with the $ sign, followed by the name of the variable

Variable names must start with a letter or an underscore

Variable names cannot be opened with numbers

Variable names can contain only alphanumeric characters and underscores (A-Z 0-9 already _)

Variable names are case sensitive ($y and $Y are two different variables)

Note: The PHP variable name is case-sensitive!

Creating PHP Variables

PHP does not have a command to create a variable.

Variables are created the first time they are assigned a value:

PHP is a loosely-typed language

In the example above, please note that we do not have to tell the data type of the PHP variable.

PHP automatically converts the variable to the correct data type based on his value.

In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable.

PHP variable Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable refers to the part of the script that a variable can be referenced/used.

PHP has three different scope of variables:

Local (partial)

Global (globally)

Static (statically)

Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed inside the function.

The following example tests for variables with local and global scopes:

...

PHP Static Keywords

Normally, when a function finishes/executes, all variables are deleted, but sometimes I need to not delete a local variable. Achieving this requires

Further work. To do this, use the static keyword when you first declare a variable:

Then, whenever the function is called, the information stored by the variable is the information contained in the last Call of the function.

Note: The variable is still a local variable of the function.

PHP Echo and Print statements

Echo and Print differences:

echo-can output one or more strings

Print only allows output of a string return value always 1

Tip: The echo output is faster than print, ECHO has no return value, and print has a return value of 1.

PHP Echo Statement

Echo is a language structure that can be used without parentheses or parentheses: Echo or Echo ()

Display string

The following example shows how to use the echo command to output a string (the string can contain HTML tags):

PHP Print Statement

Print is also a language structure, you can use parentheses, or you can use brackets, print or print ()

Display string

The following example shows how to use the Print command to output a string (the string can contain HTML tags):

...

PHP data type

String (String) Interger (shape) float (floating point) Boolean (Boolean) array (array) object (object) null (null value)

PHP string

A string is a sequence of characters like Hello world

PHP Integral type

An integer is a number that has no decimals.

Integer rule:

Integers must have at least one number (0-9)

Integers cannot contain commas or spaces

Integers are no decimal points.

Integers can be positive or negative

Integer types can be specified in three formats for decimal hexadecimal (prefixed with 0x) or octal (prefixed with 0)

PHP floating Point type

A float is a number with a decimal point, or an exponential form.

In the following example we will test different numbers, and the PHP var_dump function returns the data type and value of the variable:

PHP Boolean type

Boolean can be TRUE or FALSE

Boolean values are typically used for conditional judgments. You'll learn more about conditional control in the next sections.

PHP arrays

Arrays can store multiple values in a variable

PHP Object

Object data types can also be used to store data.

In PHP, the object must be declared.

First, you must declare a class object using the Class keyword, which is a structure that can contain properties and methods.

Then we define the data type in the class and then use the data type in the instantiated class;

The PHP keyword This is a pointer to the current object instance and does not point to another object or class.

PHP NULL Value

A null value indicates that the variable has no value, and null is a value with a data type of NULL.

A null value indicates whether a variable is a null value. It can also be used to distinguish between null values and data nulls.

Variable data can be emptied by setting the value of the variable to null:

PHP Constants

Constants are defined and cannot be changed anywhere else in the script.

PHP Constants

A constant is a large, simple representation of a value that cannot be changed in a script.

A constant consists of an English letter, an underscore, and a number, but the number does not appear as a first letter. (The constant name does not require a $ modifier).

Note: Constants can be used throughout the script.

Set PHP Constants

To set constants, use the Define () function with the following function syntax:

BOOL Define (String $name, mixed $value [, bool $case _insensitive = false])

The function has three parameters:

Name: Required parameter, constant name, or marker

Value of the required parameter constant

Case_insensitive: Optional parameter, if set to true, the constant is case insensitive and is case-sensitive by default.

Constants are global

After a constant is defined, the default is a global variable that can be used anywhere in the entire run of the script.

PHP string

String variables are used to store and manipulate text.

String variables in PHP

String variables are used to contain values that have characters.

After the string has been created, we can manipulate it. You can either use the string directly in the function, or store it in a variable.

In the following example, we create a string variable named TXT and assign the value Hello World and then we output the value of the TXT variable:

Note: When you give a text value to a variable, remember to add single or double quotation marks to the text value.

PHP collocated operator

In PHP, there is only one string operator.

The collocated operator. Used to connect two strings together

PHP strlen () function

Strlen () function returns the length of a string (string)

Tip: strlen () is often used in loops and other functions because it is important to determine the end of the string section at that time,

PHP Strpos () function

The Strpos () function is used to find a character or a specified text within a string.

If a match is found in the string, the function returns the first matching character position, or false if no match is found.

Introduction to PHP

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.