Welcome to already the second part of my PHP tutorials.
Today, we will take a look at the variables. A variable in PHP begins with a dollar sign ($).
A variable to be used must first be defined. We will define the variable "ref" and then echo it (
see my tutorial on echo).
- Code:
-
<?php
$ref = "Radiant Eclipse Forums";
echo "Welcome to $ref!<br />";
echo "Welcome to " . $ref . "!";
?>
That would output:
- Quote :
- Welcome to Radiant Eclipse Forums!
Welcome to Radiant Eclipse Forums!
(Note: the "br" tag is line-break. Otherwise it would render on the same line.)
With forms, we can also use the information user entered (we will need one HTML file and another PHP file to process what they entered - we could use
if (empty || !isset(...)), but that wouldn't be basic any more).
The
HTML page:
- Code:
-
<form action="name.php" method="post">
Your Name:<br />
<input type="text" value="" name="name" /><br /><br />
Age:<br />
<input type="text" name="age" /><br />
<input type="submit" value="Send!" /></form>
Change "name.php" to match the name of the PHP file.
And now the PHP file:
- Code:
-
<?php
//First, we define "name" and "age" variables.
//You can skip this and use $_POST variables.
$name = $_POST['name'];
$age = $_POST['age'];
echo "Hello $name! You are $age years old.";
Here is an example of what this would output:
- Quote :
- Hello Bryan! You are 13 years old.
MUST REMEMBERs about variables:
A variable must not start with a number ($0cash is invalid).
A variable can only contain alphanumeric characters and underscores (A-Z, a-z, 0-9, _). It can't have any non-English characters such as č, š, ž, ü, ä etc.
A variable cannot contain spaces. Variables, such as
$my car are not valid. The words must be separated by capitalization ($myCar) or by underscore ($my_car) or not separated at all ($mycar).
Variables are case sensitive: $ref is not the same as $Ref or $REF.
_____
We will also take a look at strings or what you can do with Echo.
In my previous tutorial, we did a basic echo code.
- Code:
-
<?php
echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec gravida.";
?>
There are several functions we can use. It would take a very long time to write all of them, so I'm referencing some of them.
Character CountUsing the strlen function, PHP will write the number of characters in a string (with spaces). Example:
- Code:
-
<?php
echo strlen("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec gravida.");
?>
gives us
- Quote :
- 71
Locate the wordUsing the strpos function, we can establish how many characters are before a certain word. For example:
- Code:
-
<?php
echo strpos("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec gravida.","amet");
?>
This will output "22".
EncodeThe most popular encoding is MD5. Many forums, including this one (unless Forumotion guys got us screwed) use it. It's often used to encrypt passwords and cannot be decrypted. In theory. There are many decrypters, but it SHOULDN'T be possible to decrypt it. We will use a simple md5 function:
- Code:
-
<?php
echo md5("REF is awesome!");
?>
That text gets encrypted as:
758776a0f2ae2370b182669e5d01aad2
Be sure to use this (or any other encoding that can be accessed directly through PHP) whenever you store passwords in a database. If someone hacks into your database, the encryption is no good to them.
Stay tuned for more!