You've come a long way already, which has included using a bunch of existing, built-in functions. Now it's time to make our own! The first thing our PHP code does right now is read our pets.json file and decode it into an array, which we set on our $pets
variable. Let's invent a new function called get_pets()
that will do this work for us and return the finished array. Putting this into a function lets us reuse this logic whenever we want, in other places in our code, without having to write it all out again. This also makes organizing and updating our code more straight-forward and simple.
Right now our pet data array is just hardcoded and not dynamic. Eventually, we're going to pull this array from a database and let users add new pets to it. But before we get there, let's pretend someone has given us a file that contains all of adorable pets available on airPupnMeow.com. Our goal will be to read that file and turn its contents into a PHP array that looks just like the one we're creating now by hand. Once you add the sample JSON file to our project (found in the resources folder of the sample code), you'll learn how to open and read the file, and then decode the JSON into an array. You're also going to learn a little bit about debugging when working with files and file paths, and be introduced to PHP booleans.
We're excited to announce a new partnership with KNP Labs to provide our members with expert PHP and Symfony2 lessons.
PHP for Beginners Part 1
CourseEvery key in an array is either a string or a whole number, which we programmers and mathematicians call an integer. And that's the end of the story: array keys are only ever strings or integers in all of PHP. But each value in an array can be any type of PHP value. So far we know three data types in PHP: a string, a number and an array. This means that we can have multi-dimensional arrays: an array with another one inside of it. Multi-dimensional arrays are actually pretty common and easy to use. In this lesson we're going to expand our existing array by putting the pet details inside the array. Once we have the new data in the array we'll look at how we access this nested information, and how we can count what we have in there.
Additional resources
We have a basic array for our pets, but we can make these much more friendly by specifying our own array keys. We'll do just that, and we'll also see how to add new items to an already existing array, without having to go in and manually add it to the array in the code.
Additional resources
We've seen how we can create variables and set each to a string or a number. We've also used functions like rand and strtoupper
to return numbers and strings. Let's talk about a third type of variable in PHP: an array. An array represents a group of things, like 5 random numbers or 3 strings. They're a really important type of data in PHP, and you'll be using them often. In this lesson we'll create an array, and then use foreach to loop through the array so we can complete a repetitive action for each item in the array. We'll also look at how you can access a specific item in an array, explaining keys and indexes.
Additional resources
We already know what to write when we want to use some PHP code, how to set a variable, and how to print things. Like most languages, PHP also has functions that make it easier to do more complex tasks. Instead of you needing to figure out all of the logic to accomplish something, like randomizing a number, you can just use an existing PHP function, like rand(), to do the work for you. In this lesson, you'll learn how to use a function, what arguments are and how they work, and use some examples in our demo site code.
Additional resources
Let's Write Some PHP
FreeSo you want to become a PHP developer. And not just any ole' programmer, but someone who writes great code and can work on exciting projects. Well then, this series from our partners at KnpUniversity is for you! You'll learn how to develop with PHP from the very beginning, with a real project, and coding exercises throughout the lessons so you can practice immediately.
In this series, we'll learn PHP from scratch by building a real website. This means you'll learn the practices used by developers to build really cool things, and not just a bunch of theory. We're going to build a site that we're calling AirPupnMeow.com. Imagine a site like Airbnb.com, except where people rent cute pets instead of apartments. If you're looking for companionship without all that responsibility of walking your dog every morning and bringing a bag to pick up his... uh gifts, then this site would be for you! Ok, the idea might be kinda silly, but that hasn't stopped startups in the past!
In this first lesson we are going to dive right in and create our first PHP file, play around with variables, and also look at what happens when we make an error, and how to read the error message we get. If you want to follow along with the steps here, you will need access to a web sever where you can write PHP files. You can get set up quickly with a number of local development servers. We have lessons that cover material for different operating systems in our Development Environments topic page.
Throughout this first series of PHP for Beginners, you are going to learn to:
- Use functions, and variables
- Work with arrays, loops, and if statements
- Read and update files
- Handle JSON
- Set up your computer to run PHP
- Create your own functions
- Organize your project into multiple files
- Create a simple layout
Additional resources
Awesome Support, Now with More Awesome
Blog postWe're always striving to provide great support to our current and prospective members. To that end, we're always looking for better ways to listen and keep track of our what our customers are saying — whether they're new, long-time members, or moving on.
Drupalize.Me Podcast Episode 46
Blog postFor this week's episode on the podcast, 46: Updating the Drupalize.Me Video Experience, Addi is joined by Joe Shindelar, Justin Harrell, and Amber Matz to discuss the why, what, and h
New Video Tutorials
In order to print out the pane title and settings form values as class names in our pane template file, we need to thread the pane object and settings array through a theme function so that they will be available to print out in our pane's template file.
In this lesson, we will:
- Walk through the pane theme function
By the end of this lesson, you should be able to implement a theme function for a panel pane.
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."
Now we have all the code in place for both our custom module and our style plugin. It's time to put it all together into our pane template file.
In this lesson...
- Connect values from settings form to template file
- Utilize values from pane object
- Utilize values from submitted settings form
All the code for the module and plugin is contained in the Resources section of this lesson. It's now your turn to create your own CTools Style Plugin!
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."
The sole purpose of our custom module is to implement a hook that will tell the CTools API that we have a plugin. Next, in our plugin's ".inc" file, we'll walk through the extensive $plugin
array, understanding how the keys and values of this array correspond to functions and parameters inside the plugin.
In this lesson, we will:
- Hook into CTools inside custom module
- Explore
$plugin
array
By the end of this lesson, you should be able to implement the correct hook for CTools and understand how to customize your own $plugin
array.
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."
Before we dive into the code of the module and plugins, let's set up the files and directories in a meaningful structure that's both scalable and one that will ensure that our plugin is disoverable by the CTools API.
In this lesson, we will:
- Create module files and directories
- Create plugin files and directories
By the end of this lesson, you will have all of the files created with a proper structure, ready for editing.
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."
The selling point of a CTools Style Plugin is the settings form. By providing a settings form to the site editor who can then change the style of the page using a pre-approved set of styles, you can both empower and provide appropriate constraints.
In this lesson, we will:
- Use the Form API
- Build a Styles Settings Form
By the end of this lesson, you should be able to build a settings form for your CTools Style Plugin.
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."
A CTools Style Plugin allows a developer to provide a settings form and a template file that can be chosen and configured by a site administrator using the Panels "Style" interface.
In this lesson, we will:
- Explore Panels' Style Interface
- Identify Default Panels Styles
- Introduce Demo Style Plugin
By the end of this lesson, you will understand how to access Styles in Panels and why you might want to create your own custom Styles interface for your site's editors to use.
Additional resources
The code for this plugin and module is located in sites/all/modules/demo_panestyles. See Companion Files to download the Files export, which also contains a demo site for Lessons 8-19 of Building Websites in Drupal 7 with Panels. Log in at /user with username "admin" and password "admin."