PAWDD E-Training
26May/100

Evaluating Variable Status

While using php you will need to evaluate or test your variables using conditional statements or functions. Or by using functions in your conditional statements. there are a few functions that can assist us to determine whether our variables have a value assigned to them, or to check if they are empty or not. They are as follows:

  • isset()
  • empty()
  • unset()

isset() Takes your variable as an argument and returns TRUE if it has a value or FALSE if it does not.

empty() Takes your variable as an argument and returns TRUE if it is empty or FALSE if it is not.

unset() Will allow you to take any set variable and obliterate it by removing its value and the variable from memory all together.

Remember: You can reverse the functions output by placing the ("!" NOT) operator in front of it.

<?php
  $myVar = "test";
 
  IF !empty($myVar) //normally empty would return FALSE because we set or variable, the "!" reverses this to TRUE
  {
    echo "I am set"; //This will be returned
  }else{
    echo "I am not set"; //This will NOT be returned
  }
?>
26May/100

Functions for Variables

Variables need special attention. Especially when you are using the ===(identical too) comparison operator, which will ensure you have a stricter set of conditional statements. They are as follows:

  • gettype() will return the a string containing the type name or "unknown" if it is not one of the following types (integer, double, string, array, or object)
  • settype() will set a variable to a specific type that is specified. takes two arguments.. (variable, type)
  • is_array() will return true or false dependant on whether the variable passed is an array
  • is_double(), is_float(), is_real() (all equivalent) will return true or false dependant on whether the variable passed is an integer with a decimal or negative
  • is_long(), is_int(), is_integer() (all equivalent) will return true or false dependant on whether the variable passed is an whole integer
  • is_string() will return true or false dependant on whether the variable passed is an string
  • is_object() will return true or false dependant on whether the variable passed is an object

These variable functions can be useful prior to working with a variable that needs to be of a specific type. This can ensure your programs do not crash.

26May/100

Employing PHP operators

PHP operators are symbols that we can use to manipulate variables and their respective values. Using the operator in this way is called using operators in operations.

The item that the operator acts upon is called the operand. There are several types of operators in PHP below is a listing of all of the specific types of them and examples.

  • String Operators
  • Arithmetic Operators
  • Assignment Operators
  • Combined Operators
  • Reference Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Bonus Operators (bonus meaning "more")

String Operators

Operators that act upon strings

Usually referred to concatenation operators. This type of operator is used to connect two strings together.

  1. . (period) like connecting strings
  2. .= (period equal) like addition for strings

Arithmetic Operators

Operators that act upon integers

These operators are used in conditional statements acting upon integers.

  1. + (addition)
  2. - (subtraction)
  3. * (multiplication)
  4. / (division)
  5. % (modulus)

Assignment Operators

Operators that assign

Assignment operators assign something to something else, like a value to a variable.

  1. = (equal)

Combined Operators

Operators that use two operators to do one job

These operators are used as shorthand for doing more complex operations.

  1. += ($a = $a + $b)
  2. -= ($a = $a - $b)
  3. *= ($a = $a * $b)
  4. /= ($a = $a / $b)
  5. %= ($a = $a % $b)
  6. .= ($a = $a . $b)

Reference Operators

Operators that create a reference from one item to another

  1. &

Basically, $b = &$a means, that $b references the value of $a from this point forward in your script, so if $a changes below your reference, then the value of the referenced variable changes with the new assignment of $a as long as it happens below the referenced call in your script.

There is expanded explanation in the, "Avoid Replication with the PHP Reference Operator" post

Comparison Operators

Operators that compare two or more things

You can use these operators to compare two values.

  1. == (equivalent too)
  2. === (identical, including data type)
  3. != (NOT equivalent too)
  4. <> (NOT equivalent too)
  5. < (less than)
  6. > (greater than)
  7. >= (greater than and equivalent too)
  8. <= (less than and equivalent too)

Logical Operators

Operators that combine the results of logical conditions

You will use these operators in conditional statements like in the IF statement

  1. ! (not)
  2. && (AND)
  3. ¦¦ (OR)
  4. AND (AND)
  5. OR (OR)

Bitwise Operators

Operators that treat integers as a series of bits

  1. & (bitwise AND)
  2. bits set in $ AND $b are set in the result $C

  3. ¦ (bitwise OR) bits set in $ OR $b are set in the result $C
  4. ˜ (bitwise NOT)any BITS contained in $a are not set in the result $C
  5. ˆ (bitwise XOR)compares $A and $B, any bits not set in both are returned as the value of $C
  6. << (left shift) SHIFTS a variable $a LEFT $b bits
  7. >> (right shift)SHIFTS a variable $a RIGHT $b bits

Bonus Operators (bonus meaning "more")

A variety of uncategorized operators

  1. Ternary Operator
  2. Error Suppression Operator
  3. Execution Operator

Ternary Operators: act like an expression version of an IF else statement. the following in an example situation where using the Ternary Operator will be of great service to us as PHP programmers.

  • ?: (question mark colon
  • $a = condition ? value IF condition is true : value IF condition is false
  • <?php
      $sentence = $value == 1 //variable that recieves the assignment and the condition
      ? 'You have one egg.' // what $sentance equals if the condition is TRUE
      : 'You have '.$value.' eggs.'; // what $sentance equals if the condition is FALSE this is the default value
    ?>

Error Suppression Control Operator: You will use this symbol to prepended to an expression in PHP, then any error messages that could be generated by said expression will be ignored by PHP and your script will continue.

  • @ (AT)
  • <?php
      @require_once("file.php") //would negate the reason to require a file 
    ?>

Execution Operator: These are NOT single quotes rather the stroke combined with the tilde(˜) key on many keyboards. REMEMBER: The backtick operator is disabled when safe mode is enabled or shell_exec() is disabled. Use of the backtick operator is identical to shell_exec() function.

  • `` (backticks) must use two in conjunction
  • if you have a problem with your script and you cannot seem to locate it, use dreamweaver to search for the backtick, you might have fat fingered it accidentally.
  • <?php
      $output = shell_exec('ls -lart');
      echo "$output"; //wrap in pre tags
    ?>
26May/100

PHP Variable Type Strength

Setting Explicit Variable Type in PHP

In PHP the variable types are not explicitly set the majority of the time since PHP is not a strongly typed programming language. "Strongly typed" means that you explicitly define the variable type when defining a variables values.

With PHP you can however specify a specific type for your variable with PHP variable type casting, or type juggling. There are several ways that you can stipulate the type of variable you want. They are as follows:

  1. Type casting:
  2. settype() Function:

Type Casting

When using Type Casting the following cast values are accepted.

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL (PHP 5+)

Example of Type Casting

$myVar = 100;   // $myVar is an integer
  $anotherVar = (boolean) $myVar;   // $anotherVar is a boolean

settype() Function

When using the settype() Function the following values are accepted.

  • "boolean" (or, since PHP 4.2.0, "bool")
  • "integer" (or, since PHP 4.2.0, "int")
  • "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double")
  • "string"
  • "array"
  • "object"
  • "null" (since PHP 4.2.0)

Example of settype()

$myVar = "50var"; // type is auto set to string
    $anotherVar = true;   // type is auto set to boolean
    settype($myVar, "integer"); // $myVar is now set to 50 type:integer
    settype($anotherVar, "string");  // $anotherVar is now set to "1" type:string
25May/100

Adobe Creative Suites (CS5, CS4, CS3)

Here you will find all things concerning Adobe Creative Suite 5, Creative Suite 4, and Creative Suite 3.

These topics will cover less and less of creative suite 3, and when the time comes, I will also cover topics concerning Adobe Creative Suite 6.

When Adobe Creative Suite 6 is issued I will stop support of Creative Suite 3.

Adobe Creative Suite 5 will be my focus point and many if not most of the posts here will be surrounding Adobe Creative Suite 5. This is because I have purchased CS5 Master Collection. CS5 is the tool I use most often. The reason for this is because CS5 offers all of the features of CS4 and CS3 plus many new features not offered in the earlier suites.

Basically Adobe Creative Suite 5 tutorials, tips, and tricks will be universal to all older editions of the adobe creative suite. When any topic or tutorial is specific to CS5 or using features that are more difficult to do in older revisions, I will point this out in the post. Potentially I will direct you toward topics here that will explain how to do a task in an earlier creative suite revision or place links to other sites where you can obtain the information you will need to complete a task.

25May/100

Books I have Read…

This is an incomplete list of the books I have read. However, it is a work in progress.

Below you will find a list of books on specific topics that over the years I have either read in their entirety, or for the most part have read all of. This is NOT a list of recommended reads, however this is a list of books I found useful for whatever reason. Mostly I read in digital format, but I plan to have a library in my future home consisting of many of the best of these books listed below.

Any book title listed in BOLD is a book I love and most likely have a hard copy of. These books are books I suggest you get, as I though enough of them to obtain a real copy of the book for either reference or just because the information contained therein is worth keeping. The others I obtained from friends or by going to my local library.

Programming

PHP, JavaScript, SQL, MySQL, Database Design, ActionScript

  1. PHP Black Book comprehensive problem solver,
    author: Peter Moulding, ISBN: 1-932111-09-3
  2. PHP and MySQL Web Development,
    author: Luke Welling & Laura Thompson, ISBN: 0-672-32525-x
  3. PHP 5, in easy steps
    author: Mike McGrath, ISBN: 0-7607-6331-3
  4. PHP 6 and MySQL 5 Visual quick start guide,
    author: Larry Ulman, ISBN: 978-0-321-52599-4
  5. SQL in easy steps in easy steps,
    author: Mike McGrath, ISBN: 0-7607-7139-1
  6. JavaScript & AJAX visual quick start guide,
    author: Tom Negrino and Dori Smith, ISBN: 0-321-43032-8
  7. PHP and MySQL for dynamic web sites,
    author: Larry Ulman, ISBN: 0-321-18648-6
  8. Concepts of Database Management,
    author: Phillip J. Pratt & Joeseph J. Adamski, ISBN: 978-1-4239-0147-1

HTML, XHTML, CSS

Hyper Text Markup Language, Extensible Hypertext Markup Language, Cascading Style Sheets

  1. The best practice guide to XHTML & CSS,
    author: Patrick Griffiths, ISBN: 978-0-321-31139-9
  2. XHTML, in easy steps
    author: Mike McGrath, ISBN: 0-7607-4795-4

Adobe

Adobe CS2 Programs, Adobe CS3 Programs, Adobe CS4 Programs, Adobe CS5 Programs

  1. Lynda.com Adobe Flash CS3 Professional,
    author: Todd Perkins, ISBN: 978-0-321-50983-3
  2. Dreamweaver CS3, with CSS, AJAX, and PHP
    author: David Powers, ISBN: 978-1-59059-859-7
  3. Adobe Photoshop CS3 Classroom in a Book
  4. Adobe Photoshop CS4 Classroom in a Book
  5. Adobe Photoshop CS5 Classroom in a Book
  6. Adobe Flash CS3 Classroom in a Book
  7. Adobe Flash CS4 Classroom in a Book
  8. Adobe Flash CS5 Classroom in a Book
  9. Adobe Photoshop CS3 Classroom in a Book
  10. Adobe Illustrator CS3 Classroom in a Book
  11. Adobe Illustrator CS4 Classroom in a Book
  12. Adobe Illustrator CS5 Classroom in a Book
  13. Adobe InDesign CS3 Classroom in a Book
  14. Adobe InDesign CS4 Classroom in a Book
  15. Adobe InDesign CS5 Classroom in a Book
  16. Adobe Fireworks CS3 Classroom in a Book
  17. Adobe Fireworks CS4 Classroom in a Book
  18. Adobe Fireworks CS5 Classroom in a Book

Design

  1. Book
  2. Book
  3. Book

Pleasure

Anything that might interest me

  1. Handbook, Pricing and Ethical Guidelines,
    author: Graphics Artist Guild, ISBN: 978-0-932102-13-3
  2. Every book ever published by Steven King
    author: Steven King
  3. The Feynman Processor,
    author: Gerald J. Milburn, ISBN: 0-7382-0016-6
  4. Business Math Demystified,
    author: Allan G. Bluman, ISBN: 978-0-07-146470-3-3
  5. A Journey Through Time,
    author: Jay Barbree & Martin Caidin, ISBN: 0-670-86018-2
17May/101

Increment and Decrement Operator

As you have probably thought prior to coming here, "I wish there was a way to increase or decrease a number without the need to use plus and minus operators over and over." Well if that was what your were thinking, then your idea has been realized with PHP "++"Increment and "--"Decrement.

This works the same in PHP and in JavaScript!

The way all this works is that normally to increase or decrease a number by one, you would need to do the following:

++/- - Pre-Increment / Decrement

<?php
 
$a = 10; 
//we need to increase $a by 1, normally we would do the following...
$a = $a + 1; // $a is now 11
 
//we need to decrease $a by 1, normally we would do the following...
$a = $a - 1; // $a is now 9
 
//There is a better way
$b = 10;
++$b //$b = 11 because we Incremented it by 1 first, by using the Increment operator "--" then returned its value
--$b //$b is 10 again because we first decremented it by 1 using the decrement operator "--" then returned its value
 
//The value of $b has literally changed, since the operator appeared prior to our variable
 
?>

With the above example showing how to increment and decrement a number by one, there is a little bit of a tricky situation happening behind the scenes. Since we placed the increment and decrement operator prior to the variable, in actuality behind the scenes PHP parser see the following :

PHP sees ++$a or --$a and literally, what takes place is that the PHP parser takes the value of $a and increments or decrements it by one, and then returns the value of $a incremented or decremented. The value of $b has literally changed, since the operator appeared prior to our variable, and that altered variable is then applied immediatly



However, in the following example something a little different takes place... Consider the following example where we move the increment/decrement operator after the variable we are working with.

Post-Increment / Decrement ++/- -

<?php
 
$a = 10; 
//we need to increase $a by 1, normally we would do the following...
$a = $a + 1; // $a is now 11
 
//we need to decrease $a by 1, normally we would do the following...
$a = $a - 1; // $a is now 9
 
//There is a different way with different results than the prior example
 
$b = 10;
echo $b++; //what will echo is 10, however if we echo it again it will show 11
echo $b //will be 11 because it was echoed after it was incremented
 
echo $b--; //what will echo is 10, however if we echo it again it will show 9
echo $b //will be 9 because it was echoed after it was decremented
 
?>

The returned value for $b remains as it was prior to the operator acting upon it because the variable $b appears prior to the operator. Since the operator is after the variable it then makes the value chang, and when we use it a second time, it has the altered value because the operator took place after the variable.

Therefore, to make it clear; placing the operator prior to the number makes PHP act upon the variable prior to returning it. Otherwise, if the operator is placed after the variable, PHP acts upon the variable after its value is returned, meaning it returns the initial value first, and is then acted upon. Therefore, the second usage of the variable is where the alteration is noticed when the operator is placed after the variable.

17May/100

Avoid Replication with the PHP Reference Operator

Since PHP version 4+ PHP has offered us a way to reduce memory usage in our programs when assigning a variable to another variable. Normally when you assign the value of one variable to another separate variable, the variable that is getting assigned to another is then duplicated by PHP in memory to be used in the operation.

What this means for us as programmers is that if we want to take the value of one variable and assign it to another, we must fist allow PHP to duplicate the variable so that it may be assigned to another.

The reference operator must be used in conjunction with the assignment operator.

We can circumvent this activity by using what is called the reference operator "&". Here is an example:

<?php
 
$a = 10;
$b = $a; //$a is duplicated in the background by PHP so that it can be assigned to $b
// $b is no 10
 
//avoid duplicating the assigned variable using the reference operator "&"
$a = 10;
$b = &$a; //& used in conjunction with the assignment operator
$a = 15;
//$a and $b = 15 and there is only one $a
 
?>

The way this works is that even after $b is assigned "&$a" later in the script when $a changes so does $b because the variable that was assigned to $b is not a copy, but the value of $a no matter where is its as long as it is after the assignment using the reference operator.

Basically, $b = &$a means, that $b references the value of $a from this point forward in your script, so if $a changes below your reference, then the value of the referenced variable changes with the new assignment of $a as long as it happens below the referenced call in your script.

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"
		}
	?>

Dashboard

Training Categories

Shaded Pixel LLC

Monthly Archive

Page Links

PAWDD E-Training