Posts

Functions in PHP

Functions in PHP In PHP, functions are used to produce some output. Functions in PHP operate a lot like Methods do in Java. Functions can take parameters or arguments and then run code using that input. In a function that takes a parameter, you can set a default value that will be used in the case where no input is received. A "return" statement is used for methods that need to return a value to be used for something else like in another function for example. Code Example: <?php //basic example of a function. Just prints "Hello  World" when called function sayHello(){  echo "Hello World"; } sayHello(); echo '<br />'; //function that uses a loop and condition to print some out put. function printNumList(){  for($i=0; $i<=100; $i++){   if($i%2==0){    echo $i;   }  } } printNumList(); echo '<br />'; //function that takes an argument and returns a result. function addFive($inputNum){  while($inputNu...
Loops There are a few different kinds of loops in PHP. There is the standard "For" loop, the "While" loop, and the "foreach" loop. The for loop takes an initialization variable, which determines where the count will start, a condition for when the loop will break (as in what value of count will the loop break), and an increment which will change the count each iteration of the loop. The while loop sets a condition that, while true, the loop will continue to iterate. Once the condition is broken, the while loop will break. The last loop is the foreach, which will loop through an array, and for each element it will execute the body of the loop. I have a code example below for some really basic code examples for uses of the different kinds of loops. The most elaborate example is the foreach loop, for which I built an array of arrays containing product information for different products. The foreach loop goes through each item in the products array and p...
Conditional Statements In PHP, conditional statements work much like they would in Java. you have an "if" statement, "else if" and "else". Interestingly enough, the "else if" statement can be typed as "elseif" and it will still work. An "if" statement will give a condition and, if it is met, the code within that conditional statement will execute. The "else if" statement will give another specific condition to check if the "if" statement's condition has been met and will execute the code contained within it if its condition is met. The "else" statement will handle all other cases. Below is an example of code for a basic conditional statement. Conditional statements in PHP can also be used in an HTML framework as I will demonstrate later on when they are used in loops and for some other functions. The PHP code must just be put in PHP tags within the HTML. <?php $value = 11; if($...
Constants And Variables Constants and variable in PHP work much like they would in Java and many other languages. Variables are used to store values that can be used throughout the program. If you want to store a value but do not want it to be altered later, you can use a constant, which uses different syntax in PHP. Variables are set by using the $ tag in front of the variable name. The variable can then be set to any value. You do not need to declare a type like you would in other languages (int, string, double, etc.). Here is an example of code using variables and constants. <?php $value1 = 5; $value2 = 7; $value3 = 5+7; $string = "Hello World"; define('FIRST_NAME', 'Tristan');  //a constant (nameOfVariable, setValue) //constants // define('greeting', 'hello world') echo "$value1 $value2"; echo '<br />'; echo "$string"; echo '<br />'; echo FIRST_NAME; echo '<br 0/...
Hello World Program Before you can create the Hello World program in PHP,  you must be connected to some kind of web server, and a database server. You will then also need a workspace. The easiest thing to do is download "XAMPP" as it has the "Apache" web server and it is free. As for a workspace to write code, I will be using "Sublime Text". It is free and it's a quick download. You will need to go into your XAMPP configuration folder once it is downloaded and connect to 'MySQL' and 'Apache'. Then you will be able to open any file by typing in the URL "localhost/insertFileNameHere. The process to get up and running with PHP can seem a little complicated at first so feel free to comment with questions. The hello world program in PHP is very simple. The syntax looks like this: <?php echo "hello world";  ?> The program must open with PHP tags, much like a program in HTML would be embedded in <html...
PHP Invention and Background PHP was originally designed by Rasmus Lerdorf for his own personal use. That is where the original name (Personal Home Page) came from. He programmed it in C and used it at first to track visits to his online resume, but greater functionality was requested, so he expanded the program and added support for large databases. This is one of the most useful features of the language. PHP went through a phase where the name was changed briefly and it was considered a good CGI or Common Gateway Interface, but was not incredibly popular as a language. Shortly after this change, however, the PHP name was brought back and the language included what was considered at the time to be an advanced scripting language. The language has become the most widely used server side programming language. Many large websites including Wikipedia, Yahoo!, and Facebook. The language was intentionally designed similarly to C so that people who are familiar with that language could...