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.
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.
Practice on using valuable
to run php if statement
Since you just
learn how to use valuable inside (php if statement). We’re going to store data
inside the valuable, then compare if within the if statement and then echo out
the result per base on if the if statement is true or false.
We’ll create a
text type able box and we’ll use the data a visitor or user will type in. After creating the text type able box, we
will fetch the data from the text box and store it in a php valuable, and
lastly use the valuable that is containing the data, compare the data with (php
if statement), then execute the end result.
Let’s dive into
the coding aspect. Check the first html code below to understand how the input
box can be created.
<input type=”password”
name=”text-box” id=”TB” />
So the code you
are seeing above is just html text input field. I set the type to password,
name as text-box, and lastly I assigned “TB” as the id just in case you prefer
to give it a nice look. Continue reading as we’re about stepping into the fun
part of it all.
Example for php
valuable;
<?php
$text_box = ($_POST[“text-box”]);
?>
As you can see
from the above code, I get hold of the text box name and assigned it into a
valuable name “$text_box”. Now whatsoever data a user type inside the text box,
the (php if statement) will check trough to know the result to give.
php if statement
example below;
<?php
If($text_box
< 8)
{
echo ”Your
password must exceed 8 characters!”;
}else
{
echo “Your
password reach required characters!”;
}
?>
This is a very
simple coding tip. Even new developers can easily build something like this.
Note: I did not
build the complete html form. It is better you have this at the back your mind,
you should have a submit button to call the action.
So if any user
enter just six characters and hit the submit button, the system error, but if
the user type in more than eight characters, the system will push out
appreciation message.
The php if else
simply instruct the computer to first check the (if statement) to see if the
requirement is reach or not. The else statement is what comes after the (if
statement).
Beyond these if and else statements, there is more to what you can use the php valuable to do. All you need to do is to think outside the box on how or what you can use the valuable to build.
happy learning and coding life!
.png)
Comments
Post a Comment