Using Strings in PHP
Before we get into some more advanced String functions – let’s take a more in depth look at how to mix HTML & PHP as well as reviewing how the echo statement works. Inserting Line Breaks There are several ways to insert HTML into PHP code. We are going to use doing line breaks as…
Read MoreHomework: Output in PHP
Playing With PHP Let’s try mixing our HTML and PHP together as well as playing with echo. See examples below. <p>Her name is: <?php echo $name; ?> </p> <?php echo “Her name is “, $name; ?> Create a file called second.php Store a name in a variable Store the person’s age in a variable Output…
Read MoreHomework: First Functions
Write a program called functions.php. In the functions part you will have: Create a function called CanDrive() that sends a provided age to determine if you are old enough to drive. Have the function return a Boolean Create a function called SayHi() that tell you good morning/afternoon or evening depending on what parameter you pass…
Read MoreNaming Conventions for PHP & MySQL
PHP Function names should: start with a verb describe what the function does use the CamelCase naming convention Examples: function getRecords() function createUser() function viewTable() Variable names should: describe what type of information or data the variable holds be written using lowercase with underscores – no spaces! avoid using ambiguous variables like $i or $x…
Read MoreWorking with Strings
Finally, this will come in handy for the homework for Loops – the str_split function will convert a string to an array. The basic syntax is: $array1 = str_split(StringToConvert, LengthOfSegment); If you leave LengthofSegment empty -the default is 1 character. But you can change that if you want it to divide the string by 2…
Read MoreTypes of Arrays
Surprise! PHP has three types of arrays: Indexed Associative Multidimensional Let’s go over each one. Indexed This is what you just learned – it is an array with a numeric key. What does that mean? When you access an item in the array, you do it by the index number. These are also called numeric…
Read MoreMore Multidimensional Arrays
You learned about a few kinds of arrays in the previous topic. Now let’s look at combining associative and multidimensional arrays. We’ll use the example below to talk about a few tricks when working with them. $students= array ( array( “name”=> “Margot”, “age”=> 13, “animal”=> “monkey”, “food”=> “pasta” ), array( “name”=> “Hadley”, “age”=> 13, “animal”=>…
Read MoreCreating a PHP file
Using PHP If you want an HTML file with PHP in it – create a file with the .php extension – you’ll be able to write HTML, PHP, JavaScript all in one file. Just like with JavaScript – you can embed PHP directly in HTML or call it via the <script> tag or via the…
Read MoreUsing Functions
Once you have created a function, how do you use it? Calling a Function You must always make sure there are a set of parenthesis after a function – even if the function has no parameters. If it has parameters, they are in the parenthesis separated by commas. First, let’s create three functions in PHP…
Read MoreCleaning Up Form Inputs
This bit of code allows you to sanitize any input from the user to make sure it does not contain anything harmful or allow any hacking of the site. Note: You can take the comments out – those are just to explain it to you! Add this function to your functions.php file. function clean_input($str){ //connects…
Read More