Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Define a Variable in PHP

what is variable in PHP

Variables are used to keep data in one place. Such as string of text numbers, etc. The value of a variable can change on the browser of a script. Here are some important things to know about variables.

  • In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
  • After declaring a variable it can be reused throughout the code.
  • The assignment operator (=) used to assign value to a variable.

Creating PHP Variables

A variable ($) in the first sentence starts with this symbol, followed by the name of Valiabale,
As given in the example below

After the first given above, the value of $ txt will be Hello World! The value of the next $ X will be 5, and the value of the next $ y will be 10.5

Note: When we assign a text value to a variable, we must put a quote around the value.

PHP Variables

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 ($age and $AGE are two different variables)

Output Variables

The PHP echo statement is often used to output data to the screen.

The example. I have given below is how to output the variable

Example:

The following example will output the sum of two variables:

PHP Variables Scope

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

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has three different variable scopes:

  • local
  • global
  • static

local scope

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Example

Global Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

Example:

Static Keyword:

Generally, when we write a function completely, all its variables are removed.Sometimes we want no local variable to be removed, we need static for further work,

When we first declare a variable, in this case we use the static keyword

As i explained in the example below

Example:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.