Home Backend Development PHP Tutorial photoshop learning tutorial PHP learning PHP variables

photoshop learning tutorial PHP learning PHP variables

Jul 29, 2016 am 08:33 AM

PHP variables
  PHP3 supports the following types of variables:
(1), internal variables
  Mainly include integers, float-point numbers, strings, arrays, and objects.
1 Initialize variables
To initialize a variable in PHP, you just need to simply assign a value to it. For most types, this is the most straightforward. For arrays and objects, other methods are available.
2 Initializing an Array
An array can be assigned using one of two methods: using a series of consecutive values, or constructing it using the array() function (see the Array functions section).
To add consecutive values ​​to an array, you only need to assign the value to the array variable without a subscript. This value will be added to the array as the last element of the array.
Example:   $names[] = "Jill"; // $names[0] = "Jill"   $names[] = "Jack"; // $names[1] = "Jack" Similar to c and perl,
Array subscripts also start from 0.
3 Initializing the object
To initialize an object, you need to use the new statement to create a variable of this type.
class foo {     
function do_foo() {     
echo "Doing foo."; }      $bar = new foo;
The scope of a variable is its validity scope. For most PHP variables there is only one scope. Use local variable scope in user-defined functions.
Variables used within functions are set to local variables by default. For example: $a=1; /* global scope */
Function Test() { echo $a; /* reference to local scope variable */ }
Test(); This program will not output anything because echo The statement wants to output the local variable $a, but $a within the function has never been assigned a value.
You may notice that this is a little different from the C language. In C, global variables can be directly referenced within the function unless it is overwritten by a local variable.
And this makes it possible for people to modify the value of global variables without noticing. In PHP, global variables must be used explicitly within a function.
For example:    $a=1;    $b=2;    Function Sum() {     global $a,$b;    
 $b = $a + $b; echo $b; The above program will output "3 ".
By declaring $a and $b as global variables inside the function, all required variables refer to the global world. There is no limit to the number of global variables that a function can manipulate.
Another noteworthy aspect of scope is static variables.
A static variable exists in a local function, but its value is not lost when the program leaves the function.
Consider the following example: Function Test() { $a=0; echo $a; $a++; }
This function is useless because every time it is called, it first sets $a to 0 and then prints "0 ". The self-increment of $a++ has no effect because the variable
$a is released after the function call ends. To make the counting program count effectively without losing the current counting result, $a must be declared as a static variable:
   Function Test() {    static $a=0;    echo $a;    $a++;
   } Now, every Every time the Test() function is called, it will print the value of $a and increase its value. Static variables are essential when using recursive functions.
A recursive function is a function that calls itself. Be very careful when writing recursive functions, because the number of loops is uncertain. You must ensure that there are sufficient conditions to end the recursive process. Here is a simple recursive function to count to 10:
   Function Test() {     static $count=0;    $count++;    
echo $count;    if($count < 10) {      Test();   }
(二) Dynamic variables Sometimes it is more convenient to use variable variable names. That is, a variable name that can be dynamically assigned and used.
The assignment statement of an ordinary variable is such as:   $a = "hello";   A dynamic variable refers to the value of the variable as the name of a new variable.
In the above example, hello can be used as a variable name by double $.
Example:   $$a = "world"; At this point, two variables are defined and stored in the PHP symbol tree: the content of $a is "hello", and the content of $hello is "world".
Therefore, the display result of the statement: echo "$a ${$a}"; is exactly the same as: echo "$a $hello"; (3) PHP external variables 1. HTML form (GET and POST)
When a form When submitting to PHP3 script, PHP will automatically get the variables in the form.For example:    
   Name:
When "submit" is pressed, PHP3 will automatically generate the variable: $name, which contains all the content entered by the user. 2. IMAGE SUBMIT variable name
When submitting a form, you can replace the standard submit button with an image through the following markup: When the user clicks on the image,
Two additional variables sub_x and sub_y will be sent to the server along with the form superior. It contains the coordinates of where the user clicked on the graph.
Experienced people may notice that the name actually sent by the browser contains a period instead of an underscore, but PHP automatically converts the period into an underscore.
3、HTTP Cookies
PHP supports HTTP cookies. Cookies store data in the client's browser to keep in touch with the user or authenticate the user's identity.
You can use the setcookie() function to set cookies. Cookies are part of the HTTP request header, so the SetCookie() function must be called before any output data is returned to the user's browser. It is similar to the limitation of the Header() function. Any cookies returned from the client will be automatically converted into standard PHP variables, just like data for GET and POST methods.
If you want to set multiple values ​​​​in a cookie, add [] to the name of the cookie,
For example: SetCookie("MyCookie[]","Testing", time()+3600);
Note: New The cookie will overwrite an existing cookie with the same name in your browser unless they have a different path or domain.
4. Environment variables
PHP automatically converts environment variables into ordinary variables.
echo $HOME; /* Shows the HOME environment variable, if set. */
Although information from GET, POST and Cookie structures will also be automatically converted into PHP variables, it is best to explicitly read them from the environment variables to ensure you get the correct value.
Use the getenv() function for this. Variables can also be set via the putenv() function.
Variable type conversion
PHP does not require (and does not support) explicit type declarations when defining variables; the type of a variable depends on the type of its value.
In other words, if you assign a string value to the variable var, var becomes a string variable. If you assign an integer value to var, it becomes an integer variable.
An example of PHP automatic type conversion is the addition operator '+'. If any operand is of double type, all operands are calculated as double type, and the result is also of double type. Otherwise, all operands are calculated as integer types, and the result is also of integer type. Note: The type of the operand itself does not change;
Type conversion is only done during calculation $foo = "0"; // $foo is a string (ASCII 48)   $foo++; // $foo is the string "1" (ASCII 49)
  $foo += 1; // $foo is now an integer (2)   $foo = $foo + 1.3; // $foo is now a double (3.3)   
$foo = 5 + "10 Little Piggies"; // $foo is a double (15)   $foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
To change the type of the variable, you can also use the settype() function.
1. Forced type conversion
Forced type conversion in PHP is the same as in C: write the desired type name in parentheses before the variable that needs to be typed.
$foo = 10; // $foo is an integer   $bar = (double) $foo; // $bar is a double   
The allowed casts are:    (int), (integer) - cast to integer    (real) , (double), (float) - cast to double   
(string) - cast to string    (array) - cast to array    (object) - cast to object
Note: The brackets can contain tabs or spaces, the following function will be Calculation: $foo = (int) $bar; $foo = (int) $bar;
2. String conversion
When a string is calculated as a numerical type, the value and type of the result are determined as follows.
If the string contains any '.', 'e', ​​and 'E' characters, it is calculated as a double type. Otherwise, it is calculated as an integer type. ​
This value is calculated from the beginning of the string. If the string is a legal number, this value is used, otherwise the value is 0.
A legal number is a sign bit (optional), followed by one or more digits (it can also contain a decimal point), followed by an optional exponent.
The exponent is an 'e' or 'E' followed by one or more digits. $foo = 1 + "10.5"; // $foo is a double (11.5) 
$foo = 1 + "-1.3e3"; // $foo is a double (-1299)  $foo = 1 + "bob- 1.3e3"; // $foo is a double (1) 
$foo = 1 + "bob3"; // $foo is an integer (1)  $foo = 1 + "10 Small Pigs"; // $foo is an integer (11)  
$foo = 1 + "10 Little Piggies"; // $foo is a double (11); the string contains 'e'
The above introduces the PHP variables of photoshop learning tutorial PHP learning, including the content of photoshop learning tutorial. I hope it will be helpful to friends who are interested in PHP tutorial.

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

See all articles