Friday, February 28, 2014

Conditional Logic in PHP

This week's post is a bit shorter, due to number of midterms that have demanded my attention away from learning PHP. However, I think that it would be at least helpful to share a few comments on conditional statements within PHP.

At the heart of conditional logic in PHP is the If statement
The logic behind the if statement in PHP matches that of most other languages. If this condition is met, then execute this  code.

The syntax is as follows:
if ($t<"10")
  {
  echo "Hello World!";
  }

In this particular IF statement, the  "Hello World" text will only be displayed if the variable t is less than 10.

If statements are sometimes supplemented with else and elseif clauses to help specify alternate routes of action when the "if" test is false.

if ($x=="10")
  {
  echo "Hello World!";
  }
elseif ($x=="20")
  {
  echo "How are you, World?";
  }
else
  {
  echo "Goodbye World!!";
  }

In this example, the text "Hello World!" will only be executed if x is equal to 10. If x is not equal to 10, but equal to 20, the text "How are you, World?" will be displayed. If x is not equal to 10 nor 20, the the text "Goodbye World" will be displayed.

What is interesting about the if statement in PHP is that it is absolutely identical to the if statement syntax in Java. So which language copied which? Both languages were debuted 19 years ago in 1995, so it is likely that both were in development around the same time. What is more likly is that both languages borrowed some syntax, including the if statement, from a common predecessor language like C or C#.






Saturday, February 22, 2014

Numeric Functions

This week's post will take a closer look at numeric data types within PHP. Specifically, it will explore some of the built in operators and functions that are commonly used with each.

Numbers:

PHP supports all of the basic mathematical operations that are found within most other programming languages. Here are a few examples;

$q=5; 
$r=3;

-------

  • $z=$q * $r Multiplication
  • $z=$q / $r division
  • $z=$q + $r Addition
  • $z=$q - $r  subtraction
  • $z=$q % $r remainder calculation


These  calculations can be stored in a variable  for later printing/returning like in the examples above. Conveniently, PHP also allows you to directly print or return the results of the calculations:


  • echo ($x % $y); 



Interestingly enough, PHP also has quite an extensive mathematical library that can be used for more advanced calculations. Usually, when I think of a web based programming language, I don't associate it with programs that require advanced mathematical calculations. Here are a few examples of the more advanced operations;


  • $z=abs($z) Absolute Value
  • $z=sqrt($q) Square Root
  • $z=sin/cos/tan($q) Trig Functions
  • $z=ceil($q) round decimal up to nearest whole number
  • $z=floor($q) round decimal down to nearest whole number



Another interesting fact about  equations in PHP is that even the more advanced calculations are part of the PHP core library. From a user's perspective, this means that there is no importing needed. This is something that isn't often seen in other object oriented languages, as Very few of them include anything other than basic addition/subtraction/multiplication/division without importation.






Friday, February 14, 2014

Variables Part 2

In the last post, I talked about how to declare a variable in PHP. In this post, I will cover the differences between the three levels of variables in PHP.

In PHP, a given variable will exist on one of three levels:

1. Global
2. Local non static
3. Local static

1. Global Variables
If you have had experience with other programming languages before, then this might seem a bit familiar.
A global Variable is a variable that can be accesses and/or changed from every function in a particular PHP class.These variables are helpful if you need to reference the same stored value for a variety of different processes.

What is a bit different in PHP , is that Global Variables are only distinguished by their declaration location in the program. Unlike in many other languages, there is no "Global" modifier.
To ensure a variable is declared as Global, it must be declared before and outside of any functions in the program.

<?php

$X=the global variable
function LearnPHP1()
{

echo "I can see " + $X;

}


function LearnPHP2()
{
echo "I can also see " + $X;
}

>


2.Non-Static Local Variables
 A Local non-static variable have two key properties that distinguish it: itcan only be accessed and modified within the function it is defined, and any changes made to the value of the variable are discarded after the 
function  it is in  has finished executing.

A variable is declared as local if it is declared within a specific function. All local variables are considered to be non-static by default-  so it is not necessary to specify it.

<?php

function LearnPHP()
{
$X=0;
echo $X;
$X++;
}


LearnPHP();
LearnPHP();

>


Even though LearnPHP is called twice, the changes to $X from the first call are not carried over to the second call.

At the end of the second call to LearnPHP();, $X only equals 1.



3. Static Local Variables
A local static variable can only be accessed within the function it is defined, but  changes made to the value of the variable are not discarded- all changes to it carry over for the next times the function is called.

A local variable can be declared as static with the "static" modifier upon deceleration

Example:


<?php

function LearnPHP()
{
static $X=0;
echo $x;
$x++;
}


LearnPHP();
LearnPHP();

>

Since $X is declared as static, the changes made to $X in the first call of LearnPHP are carried into the second call. At the end of the second call, the value of $X is 2.


Variables Part 1

PHP and Variables

Like any programming language, PHP supports the use of variables to store data. However, there are several unique characteristics of PHP that should be remembered when working with them.

The syntax for creating variables in PHP:

  • $X=5;


1. All PHP variables must start with the '$' character.

 I agree that  it's kind of strange, but once I could see where a universal starting character would be helpful in easily identifying all variables in a program.

Examples

  • If you wanted a variable called X, you would have to create it as $X
  • If you wanted a variable called 'textstring' you would have to create it as $textstring



2. PHP is a loose language.

So you probably know that it many other programming languages declaring the type of information that a variable will hold is important. Not in PHP.  PHP automatically determines a variable's type based on the value that is first assigned.

Examples:

  • If you set your variable '$X' to equal 5, PHP will automatically store its data type as an integer.
  • If you set your variable '$textstring' to equal "Hello World", PHP will automatically store its data type as a string.


3. In PHP, declaring and assigning a value to a variable must happen in the same step.

In many other programming languages it is possible to declare a variable on one line, and then assign it its first value at another point in the program. Because PHP determines the variable type based off of the value given to it (see point 2), it is necessary that you give each variable a value during the declaration stage.

Example:

  • Allowed:  $X=5;
  • Not Allowed: $X;