learning php basics part 1
DESCRIPTION
A quick tutorial on the basics for learning phpTRANSCRIPT
PHP Basic Part 1
OverivewObjective
For beginners to learn the basics of PHP. PHP is required for using ProdigyView.
Requirements
Willingness To Learn
General knowledge of programming
Estimated Time
10 minutes
http://www.prodigyview.com
Follow Along With A Code Example
1. Download a copy of the example code at www.prodigyview.com/source.
2.Install the system in an environment you feel comfortable testing in.
3.Proceed to examples/basics/PHP_Basics_2.php
http://www.prodigyview.com
Concepts To CoverVariables
Arrays
Defines
Functions
Objects
Challenge!
Review
http://www.prodigyview.com
WelcomeBefore you start using ProdigyView, you need to have some of the basics down in PHP. This tutorial is for people who have some programming experience but are relatively new to programming in PHP.
This tutorial is no where covers everything but will hopefully cover enough to get you started on the learning process.
You will need to play around with the code to get a good understanding of these concepts.
http://www.prodigyview.com
VariablesThe best place to start in PHP is with variables. Depending on if you have used other languages like Java, C++, Objective-C you will likely be use to defining a variable by giving it a type, name and a value.
Examples:
int anumber = 1;
boolean opened = true;
String[] mystrings = new String[15];
In PHP it is a little simpler. All have to do is assign a value to a variable. Variables start with the dollar($) sign and are case sensitive. After the dollar sign, variables can either start with a letter or underscore, followed by any combination of letters, underscore or numbers.
The equal sign sets the value of the variables
DEFINESDefines are variables that are made global. This
means that the variable can be accessed anywhere in the code, including functions and in objects.
Following PHP coding standards, when setting a define, they should always be capitalized.
Once a define is set, it cannot be changed.
1. Name of the Define 2. Value assigned to the define
Variables Can Be Anything
Variables are not only values such as integers and strings but can also be object and arrays. In an more advanced tutorial we will set variables to be structures such as anonymous functions. But for now, understand that a variable can be anything.
http://www.prodigyview.com
ArraysSo now that we have variables down, we can move on to arrays. Arrays are a collection of variables. If you use arrays in other languages then you might be use to setting an array size or setting the index of an array as a numeric value. In PHP you do not have to set the array size and an index can be either a string or integer.
1. Explicitly set the index of an array
2. The array() function creates an array whose index can be assigned
Iterating ArraysAfter you get use to creating arrays, the next step is iterating through them. Iterating through an array will allow you to get the values entered into the array. Also arrays can be combined to create a new array with the values of both array.
Add two arrays to create a new array
Key is the index and value is value of the index
FunctionsNext stop, the function in PHP. Functions is code that can be executed when called. Functions contain variables and variables can passed to functions. When functions are defined outside of a class, they can be called anywhere in your code.
4.Call/Execute Function
1. Define Function 2. Represents values to be passed to the function
3. Returns a value to the location the function was called
ObjectsWe’ve covered variables, arrays and functions. All of those attributes can go inside of an object. Objects are defined by a ‘class’. When a function is inside an class, it is then referred to as a method.
Inside an class, the attributes can have three states: public, private and protected. We will get more into this in another tutorial.
Like other languages, object can be initialized or instantiated(able to be accessed) with the keyword ‘new’.
http://www.prodigyview.com
Object Example
4. Call method 3. Instantiate Object
2. Create method within class
1. Define the Class
Challenge!
Now we should have some basic understanding of PHP. To better understand variables, functions and objects, complete this challenge. Keep in mind there is no right answer.
Create an object that had methods for adding, subtracting, multiplying and dividing numbers. Store the return results in an array and iterate through that array.
http://www.prodigyview.com
The Not So Obvious1. Using ‘echo’ or ‘print’ will print/display the
value of a variable to the user.
2. The ‘return’ in a function will return a value to the place the function was called.
3. Using ‘//’ will allow you to leave comments in the code. Comments are ignored by the compiler and will not affect your code.
4. To call a method in an object, use the ->. The arrow should point to the name of the method.
Example: $object->methodName()
http://www.prodigyview.com
Review1. Variables can be anything: Arrays, Objects, String,
Booleans, Integers, etc
2. Array are data structures that hold multiple variables
3. Functions is code that completes a task and can be called from anywhere in execution
4. Functions inside an object is called a method and can only be accessed through that object
5. Objects are data structures that contain functions and variables.
6. Defines are non-mutable variables that are accessible anywhere in the code execution.http://www.prodigyview.com
www.prodigyview.com
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials