Table of Contents
Beginner to learn PHP variables, constants, arrays, functions, PHP constants
Variable declaration in PHP
Constant declaration in PHP
Array
Function
Home Backend Development PHP Tutorial Beginners to learn PHP variables, constants, arrays, functions, php constants_PHP tutorial

Beginners to learn PHP variables, constants, arrays, functions, php constants_PHP tutorial

Jul 12, 2016 am 08:57 AM
php

Beginner to learn PHP variables, constants, arrays, functions, PHP constants

Recently I have learned some PHP, and continue to record notes for easy review later. The previous JS and Swift I took two days to find time to sort it out and improve it. PHP is not as ink-filled as before, so here’s the practical stuff.

PHP is a language that interacts with the server. It is object-oriented and I don’t want to talk about other features. To sum up, it is very popular now and very practical. There is nothing wrong with learning it.

Variable declaration in PHP

There is no clear type modifier in PHP, so the declared variable does not need to be given a type like other languages, or even just a var. In PHP, variables are represented by $ as an identifier, and the rest of the features are the same as JS Very similar, there is no type, the type can be changed, and everything is legal.

 $string = "JianweiWang"; //Declare a variable

$string = 5; //Modify the value of $string to 5, legal

echo $string; //5, the print result is 5

The naming rules for variable names are quite similar to those in other languages. On the basis of the $ sign, the letter starts with an underscore, and the composition can only include numbers and letters underline (/w/ in regular expressions). In the variable name No spaces allowed, case sensitive. ($_name, $name, $name1, these are all legal).

Let’s talk about the empty types in PHP, null, NULL, variables destroyed using unset, the final type will be unified to NULL.

Constant declaration in PHP

Constants play a very important role in the robustness of a program. There will be no unsafe factors such as variable reassignment due to unclear variable names due to the change of project.

PHP does not have special modifiers for constants like in other languages, or uses closures to declare a constant in js, but has special function methods to define constants.

define("PI", 3.14, true);

Three parameters, the first parameter is the name of the constant, which requires a string type, the second parameter is the value of the constant, and the third parameter is whether it is case-sensitive. If set to true, it is not Sensitive, the default is false, generally no one is so boring and insensitive - -.

So the above statement is a constant with a value of 3.14. It is not allowed to be modified. Of course, the name cannot have the $ mark, and true is set, so this constant can be accessed using PI, or it can be pI.

Sometimes, in order to prevent a constant from being redefined, we need to test whether the constant exists, so we need to use another method. defined(), the parameter is a constant name string, and the return value is a bool type. Not much to say. Got...

Array

Array is a form of data collection, and it is still a normal old rule. Here is a brief introduction. The method will be added later. There are generally two ways to define an array in PHP. One is to use the array constructor. One is the literal form.

array constructor

 $array = array(1, 2, 3, 4, 5);

Literal form

 $array = [1, 2, 3, 4, 5];

Everything is clearer in PHP arrays (there seems to be no dictionary, because associative arrays can be used), the relationship between key and value is more obvious than in JS, and the index can be modified, rather than just traditional 0, 1, 2, 3. This is also an associative array.

Initialization of associative array

There are still two methods, constructor and literal.

Type constructor

$fruit = array(
'apple' => "apple",
'banana' => "banana",
'pineapple' => "pineapple"
) ;

Literal

 $fruit = [

 'apple' => "apple",
'banana' => "banana",
'pineapple' => "pineapple"

 ];

Doesn’t this look like a dictionary? The access here is the same as JS, accessed through key values.

print_r($fruit['apple']); //You can get apples here.

Generally, the foreach method is used to traverse an array. Here is a brief introduction. You can write more by yourself.

foreach($fruit as $key => $value){

echo $key.$value.'
';

 }

This will print out

Apple apple

Banana

pineapple pineapple

Function

A function encapsulates a piece of code that can be reused, and can be called repeatedly in future use, improving the readability of the code. This is the same as in other languages, so I won’t go into details. The main points are Different ones, such as parameter tables and the like.

Function func(){};

In PHP, this is also the basic structure of a function, but it is different from some uses of JS, such as implicit triggering, etc., but the function name string is assigned to the variable name, and then triggered through the variable name. Functions, these are still very similar to the underlying implementation.

Function method(){

echo "wang";

 }

 $func = "method"; //Give the method() method to the variable $func

mechod(); //Call the method() method

$func(); //Calling $func also calls method(). This method is called a variable function

PHP has a large number of built-in functions, so I won’t go into details.

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1108740.htmlTechArticleBeginner to learn PHP variables, constants, arrays, functions, php constants. I have learned some PHP recently, and continue to take notes. , for future viewing, I will find time to organize and improve the previous JS and Swift in the past two days. P...
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
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: 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

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.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles