My Work With PHP
This page is here to show what I know about PHP, including a brief section for each part of the code I've learned.
The "If," "Elseif," and "Else" Statements
These three statements declare the conditions for something to happen so that if the written conditions are correct, the server will run the given code.
These statements allow the code to actually change small parts of the website depending on variables given in the code and what I type in the parentheses. In the code to the left, the variable "d" is the day of the week and the "if" statement says that if it's Friday to output the value of the variable "txt2." The "elseif" statement says that if it's Sunday then it should output the variable "txt." Finally, the "else" statement says that if the day is not either Friday or Sunday, it should output both the variables "txt" and "txt2." |
The "Switch" Statement
This statement is similar to the "if," "elseif," and "else" statements but this is used when one must use more than three conditions.
This Statement is also very similar to the previous trio in the way it is used: first you declare what data you are comparing to your situations, in this case I am using variable "x." then you give the conditions with the "case" statements, and set the "default" case that is used in the event that none of the "cases" are met. In this setup variable "x's" value will be compared to the numbers 1, 2, and 3. Because variable "x" has been given the value of 1 the code that is run Is the code in the first case, meaning it will output the phrase "Number 1." |
Variables and Arrays
Variables
Variables are often used to be compared to conditions in "switch" statements or in "if," "elseif," and "else" groups.
Variables are created simply by declaring them and giving them a value of a word, phrase, or a number. Variables can be changed by operations run by other parts of the code like "loop" statements. |
Numeric Arrays
All arrays create a set of variables to be used in similar ways to normal variables, though using them for mathematical functions would be confusing so they are more often used to create data that can be used in context so it does not have to be typed repeatedly.
Numeric arrays are the simplest arrays; to set them up I only have to declare the variable repeatedly with its respective content. The only difference between the variable names is a number between brackets that is different in each variable. Using this method I can use each variable as many times as I want as long as I use a different number each time. |
Associative Arrays
Associative arrays are used in the same way as numeric arrays, but more likely will be used to hold specific data on a certain subject because of the ability to use names rather than numbers.
These arrays are very similar to numeric arrays but use words inside of the brackets instead of numbers. |
Multidimensional Arrays
Multidimensional arrays create sets of data that use both numbers and names for use as variables.
To create a multidimensional array I start by declaring the variable as an array, in this case the variable "program." Next I open parentheses and state the first tree's name; in this case I name it "Adobe." Then I open parentheses and list several values for what will become the variable "$program['Adobe']." These values will be numbered in order starting at 0 and counting up. I then close parentheses and begin the name process with the "Microsoft" tree. Once that is done I close parentheses again to finish the array. This method names variables automatically based on their order and what you named the tree. In this array typing the code "$program['Adobe'][2]" into an echo command would have it output the value "Dreamweaver." |
Loops
The While Loop
Loops run a set of code that is specified, so long as the given conditions are met.
The basic while loop is the simplest example of this, coding one is simple. First I declare a variable, in this case I use the variable "n" and give it the value "1." Next I declare "while" and give it a condition, I type "$n<=9." This means the code will be run as long as the value of variable "n" is less than or equal to the value of "9." From here I add the code for it to run and the code is complete. When this code is executed it will echo the value of variable "n" and add one to "n's" value, the code will then be run until n does not meet conditions. |
The Do While Loop
The do while loop is much like the while loop but this loop runs the given code first then checks for conditions.
Firs I declare a variable, here I use variable "m" and set it to value 1. Then I give the code for it to run; the code I give here has it increase the value of variable "m" by 1 and echo the new variable of "m." Next I declare "while" and the conditions; in this case I set it so the variable "m" must be less than or equal to |
The For Loop
The for loop contains the variable, increment and conditions so one doesn't need to declare these in other parts of the code.
To make one is simple: first declare "for," then declare a variable, the condition the variable will be compared to, and an increment. Lastly add the code to be executed when the variable matches the condition. |
The Foreach Loop
The foreach loop is a loop that runs code based off of an array of variables, running them each in order given in place of the special $value variable.
A foreach loop is still simple to create: I start by creating an array, then I declare "foreach" and give the array to be used. then you declare the code to bee looped. In a foreach loop, one declares their array as the variable "$value." The code is run for each value of the array, replacing the value of "$value" with each of the array's value in the order their given. |
Functions
Basic Functions
Functions are made to run repetitive or long sets of code more easily by setting said code into a "function" to be used when that function's name is typed.
Creating a function is very simple; one only has to declare "function," write the function's name and then declare the function's code. Then to use the code I just type the function's name and the parameters to be used. |
Parameters
Parameters are parts of a function that can be change to fit a situation.
creating parameters and using them is as simple as using variables; one simply declare variables within your function's set of parenthesis and use said variables within the function's code. Then to change these variables I simply declare their values when I use the code in the same way i declared the variables themselves. |
Return Values
Return values are values that are set to output once a function is complete. This means that a function can have an output without using the "echo" command and thus can be used within an echo.
To use a return statement I simply declare "return" and then declare the value to be returned, if this is used in an echo statement it will be output like a normal variable. |
Forms
The $_GET Method
One of the data methods is the $_GET method. This method sends the data in the URL of the page, this means that a user could bookmark the page as they view it for use later, but because it displays the data in the URL it is not a good method for passwords or if the data exceeds 2000 characters.
|
The $_POST Method
The second PHP data method is the $_POST method. This method does not send the information in the URL so the page received can't be bookmarked but it can use more than 2000 characters and because it doesn't display the information it can safely be used for passwords and other delicate information.
|