Variables, arrays, and constants in PHP

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:

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

A variable name must start with a letter or the underscore character

A variable name cannot start with a number

A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)

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

Example

<?php

$txt="Hello world!";

$x=5;

$y=10.5;?>

In PHP, the array() function is used to create an array: array();

In PHP, there are three types of arrays:

-Indexed arrays - Arrays with numeric index.

-Associative arrays - Arrays with named keys.

-Multidimensional arrays - Arrays containing one or more arrays.

There are two ways to create indexed arrays:

The index can be assigned automatically (index always starts at 0):

$cars=array("Volvo","BMW","Toyota");

or the index can be assigned manually:

$cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota";

Constants are like variables except that once they are defined they cannot be changed or undefined.

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Note: Unlike variables, constants are automatically global across the entire script.

<?php

define("GREETING", "Welcome to W3Schools.com!");

echo GREETING;?>


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: