Posts

Showing posts from February, 2020

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/...