PAWDD E-Training
17May/100

Connecting the Dots with Concatenation

Within PHP there is a consistent need to connect things together to create a new entity. Such as taking two strings and creating one string. This is usually done with variables that have a string value. The most common type of concatenation is done with strings, however you can do this with numbers but the results are not what you might expect, which would be addition of the two numbers. Instead if you concatenate the number 5 and the number 6, you do not get 11, instead you get the string "56".

When you have two variables such a one for a first name and one for a last name which is most common in form input. You may want to display a welcome message to your visitor based on their login session. To do so, you might think you would have to use either their first name or last name individually, however this would be incorrect.

There are two operators that can be used to concatenate items. They are the period "." and the period/equal operator ".=".

The "." will temporarily attach one item to the next for your current operation. The ".=" will create one item out of two instead of just temporarily attaching it to the other item.

Lets take a look at the following code:

<?php
 
$first_name = 'Shaded';
$last_name = 'Pixel';
 
echo 'Hello '; //echo hello with a space after it
echo $first_name; //the first name
echo ' '; //echo a space
echo $last_name; //the last name
//the result would be: Hello Shaded Pixel
 
//OR we could use the "period" concatenation operator
 
echo 'Hello ' . $first_name . ' ' . $last_name;
//the result would be the same as above: Hello Shaded Pixel
//This way you only need one echo statement instead of four
 
//The second way is to create a new variable using the ".="
 
$hello = 'Hello';
$hello .= ' '; //a space
$hello .= $first_name;
$hello .= ' '; //a space
$hello .= $last_name;
 
echo $hello; //result is Hello Shaded Pixel
/*
notice in the second example that each operator after the first
assignment to $hello uses the ".=" meaning that the variable 
$hello, continues to obtain more and more information as 
the statements persist, the end result is one variable 
that holds the entire greeting 
*/
//FYI:
 
$a .=  $b
// is the same as 
$a = $a . $b;
 
?>

The first example of the greeting using the "." is the correct choice for this example. However, the second example using the ".=" will work. The most common use for this type of concatenation is for error messages. You can create messages throughout your script, concatenating them together using the ".=" and in the end echo out one variable that contains your error messages.

An upgrade to this idea would be to use an array of messages, and then echo out the messages from your array instead, which is a more common practice.

17May/100

Zooming in on PHP Variable Scope

Variables within php have a property that is called "scope". Scope refers to places within your php script that you can use your variable, and where you cannot.

Within PHP there are four specific types of scope.

SuperGlobal Scope
This type of scope are available everywhere within your scripts.
Global Scope
This type of scope are visible throughout your script however they are not visible inside of a function (this can be overcome by using Function Global Scope)
Function Scope
This type of scope relates to variables created within functions. Meaning that any variable created within a function is only visible to your script within the function.
Function Global Scope
This type of scope relates to variables created outside of your functions. If you declare a variable inside your function as "global", this "global" variable is not global to your entire script, however this definition of "global" inside of a function points to the value of a variable outside of your function that has been defined as global as previously defined. you would tend to think that defining a variable "global" inside of a function would make that variable available to your entire script, however this is wrong. this definition points your local function variable to the counterparts value defined outside your function (example follows)
  <?php 
	//define a variable outside of our function
	$var_two = 2; //global scope, meaning it is global to your current script
	function your_function($variable_one)
		{
			$var = "one";
			$var_two = 4; //function scope variable, local to inside the function only
			$var_three = global $var_two //$var_two is equivalent to "2" set outside the function
			//$var_three is now set to "2"
		}
	?>
17May/100

Constantly Remaining Constant

Constants are special types of variables that recieve a value and from then on can no longer be changed. this type of variable has a special place in PHP. It is normailly used when you have something that needs to be contained in a variable that iwll never change in your script, or has the potential of bing overwritten accidentially, and you wish to prevent aht from happening.

Many of the programmers in the PHP community borrow a syntax from the "C" programming language when defining a constant. That syntax is to name the constant variable in all capital letters like so.

  • A constant with a string value:
  • <?php define('YOURCONSTANT', 'your_string_value') ?>
  • A constant with a number value:
  • <?php define('YOURNUMBERCONSTANT', 100000) ?>

Naming your constants with this naming convention will help you and other programmers distinguish your constants from other variables and text. When you call your constant, like in an echo statement you do not call it with a dollar sign in front of it like you do with other variable types. This is another significant difference between constants and other types of variables within PHP.

<?php echo YOURCONSTANT ?>

The output for this echo statement will be, "your_string_value" depending on what you placed as your value. Remember that strings need to be within quotes, and numbers need not be.

17May/100

Calling all PHP “SUPERGLOBALS”

In PHP there are a set of variables called "SuperGlobals". These variables are of a special type and they carry along with them their own set of rules that define when and where they are available, called their scope.

The only difference between SuperGlobals and normal variables is; they can be seen everywhere in your scripts.

A list of these are as follows :

$GLOBALS
Holds an array of all global variables.
$_SERVER
Holds an array of all server enviroment variables.
$_GET
Holds an array of all the varialbes passed to the script through the GET method.
$_POST
Holds an array of all the varialbes passed to the script through the POST method.
$_REQUEST
Holds an array of all the varialbes passed to the script through the GET and POST methods, otherwise called user input variables.
$_COOKIE
Holds an array of all the cookie variables.
$SESSION
Holds an array of all the session variables.
$_FILES
Holds an array of all the variables associated to file uploads.
$_ENV
Holds an array of all the enviroment variables.

 

   

Dashboard

Training Categories

Shaded Pixel LLC

Monthly Archive

Page Links

PAWDD E-Training