Learning PHP Basics Part 1

10,847 views

Published on

A quick tutorial on the basics for learning php

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total views
10,847
On SlideShare
0
From Embeds
0
Number of Embeds
4,611
Actions
Shares
0
Downloads
206
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Learning PHP Basics Part 1

  1. 1. PHP Basic Part 1
  2. 2. OverivewObjectiveFor beginners to learn the basics of PHP. PHP is requiredfor using ProdigyView.Requirements Willingness To Learn General knowledge of programmingEstimated Time10 minutes http://www.prodigyview.com
  3. 3. Follow Along With A Code Example1. 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
  4. 4. Concepts To Cover Variables Arrays Defines Functions Objects Challenge! Review http://www.prodigyview.com
  5. 5. WelcomeBefore you start using ProdigyView, you need to havesome of the basics down in PHP. This tutorial is forpeople who have some programming experience but arerelatively new to programming in PHP.This tutorial is no where covers everything but willhopefully cover enough to get you started on the learningprocess.You will need to play around with the code to get a goodunderstanding of these concepts. http://www.prodigyview.com
  6. 6. VariablesDepending on if you haveThe best place to start in PHP is with variables.used other languages like Java, C++, Objective-C you will likely be use todefining 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 dollarsign, variables can either start with a letter or underscore, followed by anycombination of letters, underscore or numbers. The equal sign sets the value of the variables
  7. 7. DEFINES Defines 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
  8. 8. Variables Can Be AnythingVariables are not only values such as integers and stringsbut can also be object and arrays. In an more advancedtutorial we will set variables to be structures such asanonymous functions. But for now, understand that avariable can be anything. http://www.prodigyview.com
  9. 9. ArraysSo now that we have variables down, we can move on toarrays. Arrays are a collection of variables. If you usearrays in other languages then you might be use to settingan array size or setting the index of an array as a numericvalue. In PHP you do not have to set the array size andan 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
  10. 10. Iterating ArraysAfter you get use to creating arrays, the next step isiterating through them. Iterating through an array will allowyou to get the values entered into the array. Also arrayscan be combined to create a new array with the values ofboth array. Key is the index and value is value of the indexAdd two arrays to create a new array
  11. 11. Functions Next 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. 1. Define Function 2. Represents values to be passed to the function4.Call/Execute Function 3. Returns a value to the location the function was called
  12. 12. ObjectsWe’ve covered variables, arrays and functions. All ofthose attributes can go inside of an object. Objects aredefined by a ‘class’. When a function is inside an class, itis 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 inanother tutorial.Like other languages, object can be initialized orinstantiated(able to be accessed) with the keyword ‘new’. http://www.prodigyview.com
  13. 13. Object Example1. Define the Class 2. Create method within class 4. Call method 3. Instantiate Object
  14. 14. 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 rightanswer.Create an object that had methods for adding,subtracting, multiplying and dividing numbers. Store thereturn results in an array and iterate through that array. http://www.prodigyview.com
  15. 15. 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
  16. 16. Review1. Variables can be anything: Arrays, Objects, String, Booleans, Integers, etc2. Array are data structures that hold multiple variables3. Functions is code that completes a task and can be called from anywhere in execution4. Functions inside an object is called a method and can only be accessed through that object5. 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
  17. 17. More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×