How to Declare Variable in PHP

How to Declare Variable in PHP? It’s a basic question for PHP learners and beginners to know. Declaring a variable in PHP is a very basic thing, every PHP learner must known it. There are tons of PHP tutorials are available online. I myself have learnt lot of things online in PHP and other languages. Today I thought I myself should right some words about it. So that I am going to show you how to declare variable in PHP, let’s start.

How to Declare a Variable in PHP:

What is a Variable?

Dear readers before demonstrating how to declare a variable in PHP, I want to show you actually what a variable is? We can say in simple words variable is a memory box, like tool box to have tools in it. Like cloth box to have cloths in it. Like make up box to have make up in it, same as variable is a memory box.

Declare Variable in PHP
Declare variable in PHP

Declaring Numerical Variable:

Dear friends you can see above image, illustration of above image, first I have started PHP opening tags like this <?php

After opening tags of PHP I have declared two variables like $two and $five, I have assigned value to variable $two and 5 to variable five, then I have made a calculation. In calculation I have declared a new variable with name of seven like $seven and assigned value to it.

You can see I didn’t assigned numerical value to variable $seven, just I have assigned two variables as value to the variable $seven. At this stage you can see variable $two and variable $five are not declared as variable. They just assigned as value to the variable $seven and made calculation. Just see another example in the following image.

Declare Variable in PHP

Dear you can see here I have declared a variable $seven and then assigned numerical values to it as 2 and 5 with plus sign. Don’t worry the result of this code and above code will be the same.

Assign String to Variable:

Dear we can assign strings to variables too, to avoid repetition and saving our time. You can see, in the following image, first I have declared two variables $para_1 and $para_2. I have assigned “PHP is a web development language, widely used in designing applications.” as value to the variable $para_1. And “Examples of PHP are WordPress, Joomla, Google, GSM Arena and much more.” as value to the variable $para_2.

Then I have printed variable $para_1 and variable para_2 separately with one line gap. And then I have printed both variables $para_1 and $para_2 to make a single paragraph with the help of concatenating strings.

Declare Variable in PHP
Declaring String Variables in PHP

In this tutorial I have thought you to declare variables with two data types integer and string. We can declare variables with other data types too such as double, float and so on.

Basics of PHP:

In examples above you can see some things such as ECHO sign, I have used it for printing variable values. PHP opening tags “<?php” to start PHP script. PHP closing tag “?>” it means now PHP script is closed or finished. Double quotation mark We normally use it for separating PHP script from html coding or simple text. and finally semi colon ; it indicates that PHP coding line is closed. if you don’t use semi colon ; it will stop your script to run. It means you must use semi colon as I have used.

If you want to learn PHP from beginning, don’t worry I have written a post on it too. just read this article to learn PHP, How to Learn PHP Online with Self Study If you want to run video sharing website and you want to create your self script in php. Just read this post Best 10 PHP Scripts to Start Video Sharing Site. I hope it will be helpful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *