All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Declare Variable In PHP

Last Updated : Jul 1, 2023

How To Declare Variable In PHP

In this article we will show you the solution of how to declare variable in php, to learn any coding language basic thing is learning variable declaration as foremost step.

According to built-in rules defining variable only allow you to get error-free output, so you should know how to define variable in php.

This example code help you to define variables with any data types, so utilize it properly.

Step By Step Guide On How To Declare Variable In PHP :-

Define a variable first '$' symbol is mandatory then give name for variable as your wish.

Variable name should present in predefined form.

That name should not start with number and possible to allow underscore or contain alphanumeric letters.

Otherwise, it not accept as variable and you will get error at the run time.

Syntax :-

$variable name = value;

It will return the output respect to variable declaration and value that given as input by programmer.

<?php
$str="Hello";
$n=23;
$fn=3.45;
echo 'String variable value : '.$str;
echo '<br> Integer variable value : '.$n;
echo '<br> Float variable value : '.$fn;
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. In the sample code, first we define string variable by define $ symbol with str as variable name. Then assigned value of "Hello", always string variable accept text or number within quotes. Even number treated as string data type you can't do any calculations with that number.
  4. Then defined integer datatype variable and named as '$n', then assigned value '23' to the integer variable.
  5. Likewise, lastly created float type variable, float variable name is '$fn' value 3.45 assigned to it.
  6. Using echo statement in php we can print anything to the output platform. Each of variable values printed in elaborate manner with corresponding text.
  7. You can define variable name as your wish with correct format also values assigned to each variable. You will obtain output as per your modification.
  8. Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.

Conclusion :-

In conclusion, hope now you know how to declare variables in php with example code.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will print ‘String variable value : Hello, Integer variable value : 23, Float variable value : 3.45 as output.

With this help of sample code you can define all types of variables without flaws.

I hope this article on how to declare variable in php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪