Tutorials

Complete PHP Tutorial – Chapter 3

In this tutorial, you’ll learn about PHP Variables, Constants and Parameters.

Regular Variables

Variables are containers which we use for storing information. PHP variables work a bit differently to other languages so I suggest you pay close attention here.

A variable starts with a dollar sign ($) which is then followed by the name of the variable. That is then followed by a = sign and the value is entered after that. After the value you need a semi-colon as usual. Let’s have an example.

PHP Code:

<?php
$variable_name = 100;
?>

There are some important things you must remember when setting a variable name:

  • The name of a variable must start with a letter or an underscore.
  • Variable names are case sensitive (so $people would not be the same as $PEOPLE).
  • A variable name cannot start with a number.
  • A variable name can contain only letters, numbers and underscores.

When defining a variable you do not have to tell PHP the data type. This is where it is a bit different to other languages and shares more in common with something like python as PHP looks at what you set the variable too and gives it a datatype based on that.

Because of this, you also never need to declare a variable, it is created the moment you assign a value to it.

You can also use variables in other functions or constructs instead of directly typing in data. This comes in handy when you will be using a value more than once and don’t want to have to retype it and/or when the value of it will be changing often. For example:

PHP Code:

<?php
$name = “Yeah Hub”;
//Since I put in the quotation marks PHP knows that is a string.
echo $name;
//Now the echo construct goes and looks for variable $name and outputs it.
?>

Constants

A constant is similar to a variable except it is defined once then cannot be undefined or have its value changed. The same rules for making a constant variable name apply here.

To create a constant you use the define() function.

PHP Code:

define(name, value, case-insensitive)

Parameters:

  • name: Specifies the name of the constant.
  • value: Specifies the value of the constant.
  • case-insensitive: Should the constant name be case sensitive? This is defaulted to false.

Example:

PHP Code:

<?php
define(“HELLO”, “Hello there friend.”);
echo HELLO;

//Outputs “Hello there friend.”
?>

Example using case sensitive:

PHP Code:

<?php
define(“HELLO”, “Hello my friend.”, true);
echo hello;

//Outputs “Hello my friend.”
?>

You can also define constants by using the const variable. This works in exactly the same way but is always case-sensitive and is defined a little differently.

PHP Code:

<?php
const CONSTANT = value;
?>

Instead of using define, you simple remove the $ from in front of a variable and use the keyword const in front of the variable name.

Continue to Next Chapter – Complete PHP Tutorial – Chapter 4

Credit Goes to Mr. RYDIOO

Yeahhub.com

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

https://www.yeahhub.com/

Leave a Reply

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

four + 17 =