Variables in PHP

Source: Internet
Author: User

A variable is the result of a number, string, or function that is stored in a program or in varying amounts.

Once a variable is set, we can use it repeatedly in the script.

All the variables in PHP start with the $ symbol.

The correct way to set variables in PHP is:

$var _name = value;

Beginners of PHP tend to forget the $ symbol in front of the variable. If you do that, the variable will be invalid.

Here we create a variable that has a string, and a variable that holds a value:

<?php

$txt = "Hello world!";

$number = 16;

?>

PHP is a loosely typed language (loosely Typed Language)

In PHP, you do not need to declare a variable before you use it.

In the example above, you see that you do not have to declare the data type of the variable to PHP.

Depending on how the variable is set, PHP automatically converts the variable to the correct data type.

In a strongly typed programming language, you must declare the type and name of the variable before using it.

In PHP, variables are declared automatically when they are used.

Naming rules for variables

Variable names must begin with a letter or underscore "_".

Variable names can only contain alphanumeric characters and underscores.

Variable names cannot contain spaces.

If a variable name consists of more than one word, you should use an underscore to delimit it (such as $my _string), or start with an uppercase letter (such as a $myString).

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.