Free cookie consent management tool by TermsFeed Generator PHP Variables | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Variables

PHP Variables

Last Updated on Mar 21, 2023

What is a variable?

Well, Let me show you one:

$variable = "value";

This is an example of a php variable. Let’s deconstruct it together.

Different Parts of a Variable

$ : a variable starts with $

variable : name of the variable comes right after $

= : we assign the value to that variable with =

"value" : this can be anything. a number. a text. an object.

; : in PHP you should end an statement by ; otherwise you’ll get a syntax error

 

In php we don’t need to write the type of variables. Php does a great job figuring it out automatically

Do's and Don'ts

First Letter

The first letter of the variable name should be _ or a letter but after that you can have alphanumeric characters and underscores (A-z, 0-9, and _ ).

The variables below are all correct:

$_variable
$variable
$variabl3
$Var3

But this one is not and you'll get an error:

$3var

Case

Variable names are case-sensitive so the following variables are 3 different variables:

$var
$Var
$vAr

Assigning New Value

You can assign a new value to your variable easily

$x = “hello”;
// x is “hello”

now let’s change it

$x = 10;
// x is 10 now

As you can see not only we changed the value but also we changed the type. The first one was a string and the second one was a number. But we don’t get any errors.

Told you php does a great job taking care of types automatically.

https://youtu.be/Tmili-rOdJw

Conclusion

Now you know how to define variables in PHP.

I recommend you to open a PHP file and try defining different variables. Change their values and see if you'll get any errors.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • How to define variables in PHP
  • Different Parts of a Variable
  • Do's and Don'ts of working with variables

 

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...