PHP for Web Designers

  1. Introduction to PHP

    • How PHP makes web pages dynamic: When the browser requests a PHP website, it doesn't show the PHP code if someone clicks view source code. One difference is that if you have a time updating page, then PHP will send the time it currently is but won't update it, while JS will update it every second if programmed to do so. PHP is better showing different content depending on what has been entered in.
    • How to use PHP in a web page: Basically you use this tag in order to use it among HTML: <?php (PHP-code) ?>. When storing your PHP files, store them in the root folder of whatever testing server you have.
    • Using variables to store information: A couple of examples of variables are: name, day, distance, city. PHP variables have dollar signs ($) in front of them. Here are a couple of rules for naming variables: Always begin with a dollar sign ($); use only numbers, letters, and underscores; not spaces; you can't use a number after the dollar sign ($), and you can't use a hyphen; variables are also case-sensitive; Don't use $this, it is reserved for something else.
    • Storing numbers and text in variables: Storing numbers is easy, just put the dollar sign ($) name of the variable, and then the (=) and then then the number. For text, which are called strings, you have to put a single quote ('') or double quote ("") around the text.
    • Displaying the values of number and text variables: To display any variables content, you would use echo/print in a php tag.
    • Using functions to manipulate values: There are many functions that can be used to manipulate your variables, and you can even put some of them together into one function.
    • Adding comments to PHP scripts: The comments used are two slashes (//), or a hashtag (#) for single line, and the CSS comments (/* */) for multiline. When putting down any comment make sure that they basically describe what is going. Comments can also help with debugging because they are automatically ignored by the browser, snippets of code can be ignored to see if they have the site has the same effect with or without that snippet.
  2. Using Server-Side Includes for Common Page Elements

    • What are server-side includes?: Server side includes are a php file that puts a bit of code into a file an updates the source code accordingly but not the original file. An include isn't like an HTML file where it has to have the <!DOCTYPE> but it can have any HTMl, CSS, or PHP code and it sticks it into the main file as long as there is a PHP tag with the include keyword and the file name. For included files the name extension has no affect on what happens because the PHP is focused on finding the file.
    • Deciding which include command to use: Their are four include commands that PHP uses: include, require, include_once, require_once. the include_once and require_once commands are rarely used for web designing so mainly the include and require commands are used. The include command will include the filke but if the file can't be found then it will skip it, while with the require command, the page won't load correctly if the file doesn't exist.
    • Making sure internal links still work in an include file: When making a includes file, you are going to have to make sure that the links match up with where they are supposed to go, as in have it back back out or go into a folder to get to the right file.
  3. Using Conditions to Change Page Output

    • How PHP makes decisions: PHP helps make decisions by using what-if statements that check to see if a condition is true and if so, then code that is nested in the what-if statement will activate, and if the statement is false then the code won't activate. There is also an else-if statement that is basically a second condition. Then there is an else that is basically the default thing that happens if none of the conditions are true.
    • Changing output depending on the current time: This PHP Manual site can help with figuring out what the paremeters are for each type of time, like "H" is hour, "l" is day, and "i" is minutes.
    • Adjusting the server's time zone: On this PHP Manual site, it shows a list of continents and when you click on one of them, it takes you to another section that shows the list of countries in that continent. In the php.ini file, if you put date.timezone=(Timezone that you live in). If your company allows it and you don't have access to the php.ini file, you can do the same thing but in a .user.ini file. Another way to do it is to put in PHP tags this snippet of code: date_default_timezone_set("timezone"), but it is temporary.
    • Understanding what PHP treats as true and false: PHP and JS treat these keywords as false: FALSE and NULL (requires no quotes when typed out); a zero; a string with no space; an array with zero elements (different from JS, which regards it as true); a SimpleXML object created from empty tags. everything else is regarded as true.
  4. Working with Multiple Values in Arrays and Loops

    • Storing multple values in a variable as an array: In case you need to store multiple values into one object, an array is able to do that. Arrays are like backpacks and can hold stuff inside of them. When making an array there are two ways: $array = array("item1"), or $array = ["item1"]. To add an extra item to an already existing array without having to find the original array code, just type this out where you want it to be placed: $array[] = "value";.
    • Inspecting an array's elements: To examine an array's contents on the web page, use this php code <?php print_r($array); ?> inside of a <pre> element, to help make it easier to see them.
    • Displaying an array as a comma-separated list: If you are trying to display an array with just the echo keyword, it will provide a notice that it didn't work. To get it to actually work, you would need to use the implode() function, which can help display the array as a string. To provide spaces between the words of the array use this string before the array name: ", ".
    • Looping through an array's values: Another way to display an array's items is to use a foreach loop. with a foreach loop, you can customize the array's contents a little better than if you had used the implode() function.
    • Finding if a value exists in an array: To find an item inside of an array, you would need to use the in_array function, which takes two paremeters: the item you are looking for, and the array you are looking inside of.
  5. Getting User Input from a Form

    • Getting form input sent by the POST method: When you put the POST method for a form, PHP will make an array called $_POST specifically for the form's data.
    • Retrieving values from a URL's query string: The GET method is one that basically pulls data from wherever you are searching and shows it to you in a page that you can use. And just like with the POST method, PHP makes an array for the get method called $_GET and uses it to help with your searching.
    • Finding and extracting a substring: To find a sustring inside of another string, you would need to use this function: strpos($string, 'substring');, which takes two paremeters, the string to search and the substring you want to find. This function can help you extract the substring and display it when using echo: substr($string, startpos, length[optional]);.
    • Creating a custom function to extract part of a file name: You can create custom functions by using the function keyword at the beginning of the line of code along with a name for the function, and then implementing whatever code you need it to use in it.
    • Using PHP sessions to preserve data: One way to preserve data is to use cookies and that can help with saving data on the site. The problem with cookies is that they have to make a trip to the sr=erver each time the user refreshes. PHP sessions are a better alternative because the data is sent only once. For a session to start on a webpage, use the session_start(); code and then create session variables, which use this code $_SESSION['array']["optional" 'variable'] = 'value'.
    • Ending the PHP session and deleting the data: To end a session, you would use session_destroy and to delete any xisting data, you would simply reset the array for the session as empty.
  6. Displaying Content from a Database

    • Loading data into MySQL: MySQL is a site that is also a widely used database for websites. To load data into the site, you would need to download phpMyAdmin and set up an account and create a new database.
    • Connecting to the database: There are three APIs that are used for connecting to the database: mysqli, mysql, and PDO. The PHP documentation for APIs shows that the mysqli and PDO ones are still able to be used while the mysql has been removed.
    • Using modulo division to establish a repeating series: Modulo division finds the remainder of one number divided by another.
  7. Handling Errors

    • Dealing with PHP errors: PHP errors aren't very user-friendly for beginners in PHP, but can definently help you solve the problem if you look at what the errors say.
    • Why is my page blank or incomplete: If your page is blank or incomplete and you can't even see what is wrong, then you might need to turn the display_errors back on in your remote server opr local server and one way you can do it is to use this code directly before any other PHP code: ini_set('display_errors', '1');.
    • Tracking down parse errors: Parse errors are mostly syntax errors for whenever something is misspelled or missing a piece of it.
    • What to do with "failed to open stream": It means that a include file doesnt exist or the path to it is wrong if you have one available.
    • What does "headers already sent" mean?: It means that some information has already been sent and it won't allow any more changes.
    • What does undefined index, variable, or constant mean?: Undefined variable is when the page is trying to use a variable that doesn't exist. Index is another way to say an array key. If you forget the quotation marks for a string, then it will show an error message for a constant.
    • What on earth is T_ENCAPSED_AND_WHITESPACE?: This error mostly happens when you need to wrap some code in a curly brace and you forgot to do it, or there is a space between the first curly brace and the code inside of it.