By Acodemy
Copyright 2015
All rights reserved. No portion of this book may be reproduced mechanically, electronically, or by any other means, including photocopying without the permission of the publisher
LEARN PHP IN A DAY
The Ultimate Crash Course to Learning the Basics of PHP in No Time
Disclaimer
The information provided in this book is designed to provide helpful information on the subjects discussed. The authors books are only meant to provide the reader with the basics knowledge of a certain topic, without any warranties regarding whether the student will, or will not, be able to incorporate and apply all the information provided. Although the writer will make his best effort share his insights, learning is a difficult task and each person needs a different timeframe to fully incorporate a new topic. This book, nor any of the authors books constitute a promise that the reader will learn a certain topic within a certain timeframe.
Contents
Preface
- Introduction to the Course
Welcome to this Introductory Course to PHP. Throughout this course, you will learn the basics behind using PHP in your own projects. You will be introduced to some of the most commonly used functions and methods as well as PHP best practices. This course will mostly teach by example, emphasizing a show, dont tell approach. We will guide you through different real-world examples of PHP applications and explain every step of the process so that you understand not only how , but also why we are writing our application the way we are.
The course is structured around different chapters, where each chapter is focusing on a particular aspect of programming with PHP. At the end of each chapter, there will be a chapter summary and a chapter exercise that will test all the skills you have learned in the chapter thus far, as well as build on top of previous knowledge.
You will be presented with code at every step of the way. Code will be easily recognized as it will be separated from the rest of the text, and will be formatted differently. Here is an example of a code block:
With all of that out of the way, lets get started with the course!
Chapter One Introduction, Setup and Hello World
Introduction to PHP
PHP stands for PHP: Hypertext Preprocessor (originally it meant Personal Home Page) and is one of the most widely used web programming languages (installed on more than 250 million websites). It is similar to languages such as C. But lets not spend too much time in historical references and get to work!
Setting up Our Work Environment
In order to start working with PHP on your local computer, you need to install a development environment. Why is this so? PHP is a server-side language; so in essence, you need to install a local server on your computer in order to run PHP code. There are many solutions that provide PHP packages, but the most common form is known as a LAMP bundle. There are many LAMP bundles out there that fit different operating systems. The most common are:
- XAMPP an Apache distribution with PHP, MySQL and Perl. Support available for Windows, Linux and Mac;
- WAMP specific for Windows;
- MAMP specific for Mac;
Pick one, type the name in Google, follow the download link and instructions and youll be all set to go.
Also, you will need some form of text editor. There are many free and paid programs you can find. The one we are using for this course is called Notepad++ .
Our Very First PHP file
After having installed your LAMP bundle, you undoubtedly want to create your very first PHP file. If you havent already, make sure that you have Apache and MySQL active. To check, go to the control panel of your LAMP setup and you should see something like this:
Navigate to the installation directory of your LAMP bundle. In my case it looks something like: C:\xampp\htdoc s . htdoc s is the directory that contains all of your websites. Generally, for the sake of clarity, I like creating a website s directory inside of htdoc s and working from there. In either case, create a new folder for your website and give it a name. Try to refrain from using spaces (use _ instead). Inside that folder, create an index.ph p file. Open the file in your text editor and type the following lines of code:
All PHP code is wrapped within the PHP tags (
Chapter Two Variables
Introduction to Variables
Basics
In PHP, variables are declared using the dollar sign ( $ ):
This declares, or creates, the variable. Important to note is that before you create the variable, it is considered unset. It doesnt exist. Kind of intuitive, but this can sometimes lead to some unexpected errors as we are going to see later on.
Here we have created a strin g variable (note that we dont declare the type). Well look at the different variable types a bit later in this chapter.
Rules
There are rules to keep in mind when creating variables inside of PHP. Well list all of them and after that we will take a look at code that shows these. Here are the basic rules:
- Variable names start with a letter or underscore;
- Variable names cannot start with a number;
- A variable name can have any number of letters, underscores of numbers;
- Valid letters are all ASCII hexadecimal letters from 00 to FF;
Lets take a look at an example of code declaring variables:
If we test our file we will get the following error message:
This tells us that we have an error in declaring our variable.
Variable Types and Typecasting
Unlike many other languages, PHP does not require you to explicitly declare the variable type, rather, it determines it from the context. In other words, you dont have to explicitly tell PHP whether you want a variable to be a double precision number or a string. To help you imagine this, you can think of variables as boxes. Imagine a row of lockers (like the kind you would see at an airport or a train station). Each locker represents a variable. Each locker has its own unique number, which differentiates it from all of the other lockers. If someone tells you to open locker number 42, you would be able to find it without a problem and you wouldnt confuse it with any other locker (for example 24). In a similar way, PHP variable names are unique and each PHP variable is unique. Now, you open locker number 42, and you see that it is empty. You move on to locker 43 and see that there is a package inside it. You take the package from 43 and place it in 42. Before opening the lockers you didnt know what was in the lockers. After checking, you know what is contained inside that locker and you can manipulate it as desired. In a similar fashion, variables in PHP can contain anything you place in them. Essentially, when you are checking the type of a variable, you are checking the type of the content inside of the variable.
Next page