Basic understanding for php variables and values

I so much believe that after this article, you will be able to write php statement that can store data/values inside variables and you can also execute them to achieve your desire results.

Before we start writing our first line of codes, I will explain to you the things that are allowed and the things not allow when storing value inside variables.

php variables and data


We know that a lot of programming languages does restrict programmers from just giving variables any name, programming language like Java, want the developer to tell the language on what the variable will hold.

For example, if a variable to contain users password, you must specified that with the name you will give the variable, but unlike php, you are free to give any variable any name of your choice as the developer.

 

Php variables rules:

1.   You can give your variable any name of your choice

2.   Variables can hold both number and text

3.   Double quotes is must for any variable that will hold text

4.   A variable must start with dollar symbol ($)

5.   Sime colon is used at the end every variable

Now that we know the basic rules for variables, it is time we start going into practice. We’ll store a word inside a variable and execute it to outcome.

First thing to write out our php opening and closing tags, and then in between them, we write the rest of our codes.


Related articles:

How to create web app chat system from scratch

How to build php users registration system

How to know the difference between programming and web development


 

Example;

<?php

          $example1 = “hi Jerry!”;

          Echo  $example1;

?>

As you can see on the above, I did not do anything concerning database connection because we’re not working with DB.

First line I named a variable “example1” using a dollar symbol to mark it as a variable, then secondly, I echo out the result, which says “Hi Jerry!”.

You too can try it and give me feedback.

Again, we’re going to try another example and this time around, we’ll store numeric inside the variable instead of text.

 

Example;

<?php

          $example2 = 2026;

          Echo  $example2;

?>

 

As you can see, there is no much differences in both. Example1 uses double quotes, while example2 does not use a quote.

If you’ve read through our article on how to send users information into php DB, then storing html form input data inside php variable will never be a problem.

But I will give you a tip on how to get it done. I will not take you through creating html form. So I hope you already have your form with input fields all working. Let’s get started­­!

 

Example;

<?php

$username = ($_POST["username"]);

?>

In the above example, I created a variable name as “$username” and assigned the username input field into it. Now you need to pass whatever information inside that username input field into the php database using the variable name.


Related articles:

How to create web app chat system from scratch

How to build php users registration system

How to know the difference between programming and web development


If you still finding it difficult to send any information to your database, then you can read our post on the steps to allow users to information to DB.

Note: you must secure the input field so users can’t send just anyhow information into your database.

If you don’t security major in place, bad guys can access your site backend information.

happy learning and coding life!

Comments

Popular posts from this blog

How to stop unnecessary service charges from your network providers

Best steps to remove virus from your Computer using command prompt