{"id":149616,"date":"2016-01-19T11:00:20","date_gmt":"2016-01-19T16:00:20","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=149616"},"modified":"2018-05-23T20:37:35","modified_gmt":"2018-05-23T20:37:35","slug":"wordpress-development-beginners-php","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-php\/","title":{"rendered":"WordPress Development for Beginners: Learning PHP"},"content":{"rendered":"<p>Becoming a WordPress developer goes hand-in-hand with learning PHP, the popular server-side scripting language WordPress is built on.<\/p>\n<p>Originally created in 1994, PHP is a powerful and free open source tool for creating dynamic and interactive websites.<\/p>\n<p><span style=\"line-height: 1.5;\">This is the second\u00a0post in our five-part series for beginners, teaching you the fundamental concepts of WordPress development so you can take the leap from tinkerer to developer. By the end of the series, you will be able to create your own rudimentary themes and plugins, and flesh them out with your own features.<\/span><\/p>\n<p><span style=\"line-height: 1.5;\">In this tutorial, you&#8217;ll learn the basic syntax of PHP, the logic behind how it works, and I\u2019ll walk you through coding examples.<\/span><\/p>\n<p><em>Note: For this series, it\u2019s important that you already have a thorough understanding of HTML and CSS as both of these languages are essential building blocks when working with WordPress.<\/em><\/p>\n<p>Let\u2019s get started.<\/p>\n<p><strong>Missed a tutorial in our WordPress Development for Beginners series? You can catch up on all five posts here:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-getting-started\/\" target=\"_blank\">WordPress Development for Beginners: Getting Started<\/a><\/li>\n<li><a href=\"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-php\/\" target=\"_blank\">WordPress Development for Beginners: Learning PHP<\/a><\/li>\n<li><a href=\"https:\/\/wpmudev.com\/blog\/theme-development\/\" target=\"_blank\">WordPress Development for Beginners: Building Themes<\/a><\/li>\n<li><a href=\"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-widgets-menus\/\" target=\"_blank\">WordPress Development for Beginners: Widgets and Menus<\/a><\/li>\n<li><a href=\"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-building-plugins\/\" target=\"_blank\">WordPress Development for Beginners: Building Plugins<\/a><\/li>\n<\/ul>\n<h2>What is PHP?<\/h2>\n<p>PHP is a server-side scripting language. To understand what that means, let&#8217;s compare it to HTML.<\/p>\n<p>When you visit a simple HTML page, your browser makes a request to the server that contains it. The server figures out which file you need and sends it to you as-is. Your browser interprets the HTML code and displays it.<\/p>\n<p>In comparison, when you visit a PHP page there is an additional step. Your browser sends a request and the server finds the file(s) you need. Before sending anything back, the server processes the PHP files, resulting in a final HTML output. This is sent back to your browser, which displays it as usual.<\/p>\n<p>This is why you can look at any website&#8217;s source code and never see any PHP code, only HTML, even if the site is written in PHP.<\/p>\n<p>So why would the server need to do the processing? What&#8217;s the point of all this? It allows us to create dynamic websites and code much more efficiently.<\/p>\n<p>Let me give you two simple examples.<\/p>\n<p>In HTML everything is static. You could create a page that displays &#8220;Good morning&#8221; or &#8220;Good evening,&#8221; but you couldn&#8217;t just show one or the other, depending on the actual time. In PHP you can because the server can process your code and take the actual time of day into account. Here&#8217;s what the logic for it would look like:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"basic.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=basic.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This isn&#8217;t real code of course, but it follows the logic of PHP. The server determines which statement is true and will display only one of the headings.<\/p>\n<p>Another great example is Twitter or any other site that serves user-made content. If Twitter only used HTML, someone would have to quickly regenerate your Twitter feed each time you post \u2013 not exactly an efficient use of time. To put it into perspective, there are almost 1 billion Twitter accounts, so the company would need 1 billion HTML files, one for each user, in a folder somewhere.<\/p>\n<p>With PHP, the problem of regenerating feeds could be solved with just a handful of files (though is possible to do it with a single file) quite ingeniously. We&#8217;ll come back to this example later in this tutorial.<\/p>\n<h2>Getting Started<\/h2>\n<p>Before we begin, I want to note that in the early stages of studying PHP you might not get what it has to do with websites. This is a completely normal reaction and it&#8217;s exactly what I thought when I originally started learning PHP. The first steps are a bit abstract, but by the end of this article you&#8217;ll see the light.<\/p>\n<p>In order to test and practice what you&#8217;ll be learning in this tutorial, you will need an Apache server. This can be a test site online but it can also be a local server.<\/p>\n<p>I recommend creating a local test server. Here&#8217;s how to do it:<\/p>\n<p>First, download and install <a href=\"https:\/\/www.virtualbox.org\/wiki\/Downloads\" target=\"_blank\">Virtualbox<\/a> and <a href=\"https:\/\/www.vagrantup.com\/downloads.html\" target=\"_blank\">Vagrant<\/a>. Create a directory anywhere on your computer to store your project files. I have a &#8220;websites&#8221; folder in my user directory, in which I&#8217;ve created a &#8220;phptutorial&#8221; directory. I&#8217;ll refer\u00a0to this as the &#8220;main project directory&#8221; throughout the rest of this tutorial.<\/p>\n<p>Once you created the directory, open the terminal on Linux or OSX, or the command prompt on Windows, and navigate to the folder. If you have created the same folder structure as I have, you can type <code>cd ~\/websites\/phptutorial<\/code> on Linux or OSX. On Windows, you can type <code>cd %HOMEPATH%\/websites\/phptutorial<\/code>. Once in the correct directory, paste the following command:<\/p>\n<div class=\"gist\" data-gist=\"b5d7ecd113e3107aedbe6b7103e063fc\" data-gist-file=\"localserver.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/b5d7ecd113e3107aedbe6b7103e063fc.js?file=localserver.sh\">Loading gist b5d7ecd113e3107aedbe6b7103e063fc<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This will take quite some time to complete as it will download and install a 700MB operating system. You only need to wait it out once, so get some cocoa while it finishes.<\/p>\n<p>Once completed, you will be able to access your site at <code>http:\/\/192.168.33.21<\/code>. If you restart your computer, you&#8217;ll also need to restart your server, which you can do by navigating to the main project folder in the terminal and typing <code>vagrant up<\/code>.<\/p>\n<p>The main project file is now your root folder. Create an <code>index.html<\/code> file in there with any content and it should be rendered correctly when you visit your website at <code>http:\/\/192.168.33.21<\/code>.<\/p>\n<h2>First Steps<\/h2>\n<p>First up, let&#8217;s look at PHP tags, variables, values, and echoing.<\/p>\n<p>Create an <code>index.php<\/code> file in your main project folder and type &#8220;Hello HTML&#8221; in it. To verify it is working, visit <code>http:\/\/192.168.33.21<\/code>, and you should see the string displayed.<\/p>\n<p>A very important rule is that <strong>only content within PHP tags is considered PHP code, anything else is considered HTML<\/strong>. This is why our <code>Hello HTML<\/code> text shows up. PHP tags begin with <code>&lt;?php<\/code> and end with <code>?&gt;<\/code>. Anything in-between those tags is considered PHP.<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"hellophp.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=hellophp.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The code above shows how PHP tags are used and also shows echoing. <strong>When you echo a value you are telling PHP to display it<\/strong>. The result of the code above will be &#8220;Hello HTML Hello PHP&#8221;. The first part is considered HTML because it is not within PHP tags while the second part is echoed by PHP code. Note that you <em>must<\/em> enclose your text within quotes.<\/p>\n<p><strong>Variables allow you to store values without displaying them.<\/strong> Variables are used extensively to hold many different things. Let&#8217;s look at a basic example:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"variable.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=variable.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Here, I&#8217;ve stored the value &#8220;Daniel&#8221; in a variable called <code>$name<\/code>. Instead of echoing &#8220;Daniel&#8221; directly, I am echoing whatever the value of <code>$name<\/code> is. This seems a little redundant here but if the variable is used in a lot of places we can change everything by changing the value of the variable in one place, instead of having to overwrite it everywhere.<\/p>\n<p>Note that two lines of code must be separated with a semicolon. If your code produces an error, chances are you have forgotten to put a semicolon somewhere \u2013 a common mistake and frustration for coders everywhere!<\/p>\n<p><strong>Values<\/strong> can be of numerous types. So far we&#8217;ve only looked at one: the string. A string is a sequence of characters and must be enclosed within quotes. Another frequent one is an integer. An integer is any whole number and it should <em>not<\/em> be placed within quotes.<\/p>\n<p>One of the most important value types is\u00a0<strong>arrays<\/strong>. Arrays hold multiple values, which can all be different types.<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"values.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=values.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>As you can see, arrays are created using the form <code>array( value1, value2, value3 )<\/code>. The values should always be written as they are defined when working outside an array. Strings should be quoted, integers should not be quoted, and so on.<\/p>\n<p>I don&#8217;t want to get too much into arrays here, so before continuing please take a look at this fantastic <a href=\"http:\/\/www.w3schools.com\/php\/php_arrays.asp\" target=\"_blank\">tutorial on arrays at\u00a0W3Schools<\/a>. The most important parts of this tutorial you should read about are on how arrays can be created, how values can be added, and what an associative array is.<\/p>\n<h2>A Simple Example<\/h2>\n<p>So what can all this be used for? Right now, what we&#8217;ve looked at so far isn&#8217;t enough to whip up a website but a practical example is definitely in order.<\/p>\n<p>Let&#8217;s look at the bare bones of a simple weather forecast application:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"forecast.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=forecast.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>There are a few things that are new, but I think the example speaks pretty clearly. Let&#8217;s go through it line-by-line.<\/p>\n<p>Initially, we create two variables: once is a string holding the value &#8220;C&#8221; for Celsius, and the next variable is an integer, 29, which is intended to hold the actual temperature.<\/p>\n<p>The goals here are simple: display some text that tells us the current temperature using the value from our temperature variable in the chosen unit of measurement, celsius or Fahrenheit.<\/p>\n<p>The next line is an <code>if<\/code> statement, which is performed only if the value within the parenthesis is true. The condition is: <code>$unit == 'F'<\/code> which translates to &#8220;if the unit value is &#8216;F'&#8221;. Currently, it is <em>not<\/em>, so let&#8217;s skip the whole <code>if<\/code> block.<\/p>\n<p>On to the last line.<\/p>\n<p>The forecast is hard-coded to start with &#8220;The weather today will be great, a Sunny &#8220;. Depending on the value of our <code>$temp<\/code> and <code>$unit<\/code> variables, the ending will be different. The dots between strings and variables concatenate them or join them together.<\/p>\n<p>So we simply substitute our values:\u00a0<code>$temp<\/code> should be 29 and <code>$unit<\/code> should be &#8220;C.&#8221; So the final output is:<\/p>\n<blockquote><p>The weather today will be great, a Sunny 29C<\/p><\/blockquote>\n<p>Now, what happens if we change the <code>$unit<\/code> to &#8220;F&#8221;? First of all, the <code>if<\/code> statement kicks in because it becomes true. This means that everything between the curly braces gets executed as well. In the example, I assigned a new value to the <code>$temp<\/code> variable.<\/p>\n<p>I took the original value, multiplied it by 9, divided it by 5 and added 32 \u2013 this is the math for converting celsius to Fahrenheit. The new value for <code>$temp<\/code> should be 84.2, and the new value for <code>$unit<\/code> should be &#8220;F.&#8221; Substituting the variables with their values in the final sentence results in:<\/p>\n<blockquote><p>The weather today will be great, a Sunny 84.2F<\/p><\/blockquote>\n<p>This example is still a bit useless to us, but it starts to show the power of PHP. Note how we arrived at the final output by basically replacing variables with values. It helps to get into the mindset of &#8220;replacing placeholders&#8221; because it is essentially what PHP is about.<\/p>\n<p>In a real world example, we would retrieve the current sky conditions and instead of hard-coding &#8220;great, a Sunny&#8221;, we would use the actual conditions. We might also want to forecast for tomorrow or the day after, which would result in &#8220;today&#8221; being replaced by a placeholder as well.<\/p>\n<p>In addition, the current temperature would obviously not be hard-coded into the code, it would need to be retrieved from somewhere. Even though this adds a couple of layers of complexity, the essence remains the same \u2013 we&#8217;re grabbing data from somewhere and placing it in our output.<\/p>\n<h2>If Statements and Loops<\/h2>\n<p>We&#8217;ve already seen an <code>if<\/code> statement in action. An <strong>if statement<\/strong> evaluates the condition in the parenthesis and selectively processes code based on the result. If the result is true the code is processed within the curly braces otherwise it isn&#8217;t.<\/p>\n<p><code>If<\/code> statements can also have related <code>else if<\/code> and <code>else<\/code> statements. <code>else if<\/code> statements are always written after <code>if<\/code> statements. If the original <code>if<\/code> statement turns out to be false, the <code>else if<\/code> statement is then checked, using the same rules as before. You can have any number of <code>else if<\/code> statements.<\/p>\n<p>Finally, the <code>else<\/code> statement is evaluated if everything has been false leading up to it. You don&#8217;t have to have an <code>else if<\/code> statement within a group, you could only have an <code>if<\/code> and an <code>else<\/code> statement together.<\/p>\n<p>Take a look at the following examples for syntax and clarification:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"if.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=if.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>One more thing to note here are the rows beginning with double slashes. A double slash allows you to add a single-line comment anywhere in your code. You can add multi-line comments by starting with <code>\/*<\/code>, writing whatever you&#8217;d like and ending it with <code>*\/<\/code>.<\/p>\n<p><strong>Loops<\/strong> come in a number of shapes and sizes, four to be exact. They all allow you to run the same code a number of times in succession. You may hate them now, but they will become your closest ally when you become more proficient at coding!<\/p>\n<p>Let&#8217;s start with a <strong>foreach<\/strong> loop. A <code>foreach<\/code> loops goes through all members of an array and executes the given code. Here&#8217;s a quick example that outputs an HTML list:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"foreach.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=foreach.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The key here is understanding the content of the parenthesis, which is <code>$names as $name<\/code>. The first part is the name of the array you want to loop through. The second is the name of the variable you can refer to in order to get the value of the array member you are currently at during the loop.<\/p>\n<p>In our example, the loop will be performed three times. On pass number one, the value of <code>$name<\/code> will be &#8220;Daniel Pataki,&#8221; on the second pass it will be &#8220;Raelene Morey,&#8221; and on the last pass it will be &#8220;James Farmer.&#8221;<\/p>\n<p>If you followed my advice earlier in this tutorial and read up on arrays, you will know what an associative array is. The following example displays my details in a list, from an associative array:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"foreach-assoc.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=foreach-assoc.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The parenthesis now contains three variables: <code>$details as $label =&gt; $value<\/code>. We are going through the <code>$details<\/code> array. On each pass we&#8217;ll call the key <code>$label<\/code> and the value <code>$value<\/code>. On the second pass, the <code>$label<\/code> will be &#8220;Age,&#8221; the <code>$value<\/code> will be 30. The code results in something like this:<\/p>\n<ul>\n<li><strong>Name<\/strong>: Daniel Pataki<\/li>\n<li><strong>Age<\/strong>: 30<\/li>\n<li><strong>Twitter<\/strong>: http:\/\/twitter.com\/danielpataki<\/li>\n<\/ul>\n<p>If you want to learn more, take a look at\u00a0<a href=\"http:\/\/www.w3schools.com\/php\/php_looping.asp\" target=\"_blank\">other\u00a0types of loops<\/a>\u00a0available: <code>while<\/code> loops, <code>for<\/code> loops, and <code>do-while<\/code> loops. They are all very similar, but with minor differences.<\/p>\n<h3>How WordPress Post Lists Work<\/h3>\n<p>Let&#8217;s now take a look at an example more relevant to using WordPress: how a post list works.<\/p>\n<p>The code below does not mimic WordPress exactly, but the gist is there. Imagine posts as being parts of a big associative array. Your home page&#8217;s code simply loops through this array and places the appropriate data in the appropriate place in some HTML code:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"postloop.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=postloop.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h2>Functions<\/h2>\n<p>In a way, functions serve the same purpose as variables \u2013 a way to substitute code. Functions can take a block of code and store it for later (and multiple) use.<\/p>\n<p>Let&#8217;s look at a very simple example using our post loop above. The part where we echo the post elements will probably be used in a number of pages on a website \u2013 the homepage, category archive pages, author pages and so on. We would use the same block of code again and again. This is exactly when functions come in handy.<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"functions.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=functions.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The code above is not shorter, but any time we want to display a post in the future we can just type <code>display_post( $post );<\/code> making it a lot more efficient in the future.<\/p>\n<p>So how does this all work?<\/p>\n<p>Let&#8217;s approach this by looking at the function at the top of the code first. I&#8217;ve named the function <code>display_post()<\/code> and in the parenthesis you can see that it expects one parameter. We know that this parameter should contain post data so it would be an associative array containing the data of a single post. I&#8217;ve named it <code>$postdata<\/code>.<\/p>\n<p>Within the curly braces we include all the code we want to execute when the function is called. This is the same code as we had before, I just needed to rename all variables to <code>$postdata<\/code>, the name of our parameter, which we can use anywhere within the function.<\/p>\n<p>At the bottom of the code, you can see that displaying a post is now much cleaner, we just used <code>display_post( $post )<\/code>. Now, I bet you&#8217;re confused about the naming. Why is it <code>$post<\/code> in one place and <code>$postdata<\/code> in the other?<\/p>\n<p>Let&#8217;s go through the code.<\/p>\n<p>Functions are only run when they are called, so the first thing that actually matters in our code is the definition of the <code>$posts<\/code> array. We then create a loop, just like we did before. Within the loop, the variable that contains the data for a single post is <code>$post<\/code>, so far nothing has changed from our function-less example.<\/p>\n<p>Now, instead of having the mass of code in there, we simply defer to a function. We call the <code>display_post()<\/code> function. We know that we need to pass it some data \u2013 the data of a single post. Which variable contains that information right now? The <code>$post<\/code> variable, this comes from the definition of the loop.<\/p>\n<p>Next, scroll up to the function. It will now be executed. In the function, the first parameter is named <code>$postdata<\/code>. This simply means that within the function, we will refer to the data passed to the first parameter as <code>$postdata<\/code>.<\/p>\n<h3>Creating an Excerpt<\/h3>\n<p>Functions can have multiple parameters and default parameter values. Let&#8217;s create a function that can make any text into an excerpt by cutting it down into pieces. We want our function to be as flexible as possible, able to control the excerpt length, and a string to append to the end.<\/p>\n<p>We&#8217;ll make the first parameter the text we want to shorten, the second parameter control the length, the third the appended text.<\/p>\n<p>Let&#8217;s start by giving our function a frame to work within:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"frame.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=frame.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Note that I&#8217;ve given default values to the parameters. This means you can invoke the function without passing the second and third parameters. If you do this the function will presume you want to create a 200 character long excerpt and append three dots afterward. Let&#8217;s flesh out this function:<\/p>\n<div class=\"gist\" data-gist=\"4075ec96709c39b2e404\" data-gist-file=\"excerpt.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4075ec96709c39b2e404.js?file=excerpt.php\">Loading gist 4075ec96709c39b2e404<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I&#8217;ve added plenty of new things in there, so let&#8217;s go through each new addition in detail.<\/p>\n<p>It all starts with an <code>if<\/code> statement, which checks if the text given is actually longer than given length. It does this using a function built into PHP called <code>strlen()<\/code>. This function takes a string as its parameter and returns its length. If the <code>$text<\/code> length is less than <code>$length<\/code> the text is simply returned \u2013 we&#8217;ll talk about what returning means in a moment.<\/p>\n<p>Otherwise, we create an excerpt using the <code>substr()<\/code> function. This function cuts up a string and requires three parameters: the text to take a chunk out of, the starting position of the chunk, and the length of the chunk. We want to start at the beginning of the text and cut off <code>$length<\/code> characters, which is why I&#8217;ve added 0 and <code>$length<\/code>.<\/p>\n<p>Finally, we add the append string to the end. I&#8217;ve used the <code>.=<\/code> notation to do so. This is the same as writing <code>$excerpt = $excerpt . $append<\/code>. In the last line, I returned the <code>$excerpt<\/code> variable.<\/p>\n<p>So what is returning? So far we&#8217;ve mostly been echoing things, but in many cases we don&#8217;t want to display the result of our &#8220;calculations.&#8221; The <code>strlen()<\/code> function is a good example. You don&#8217;t actually want to display the length of the <code>$text<\/code> variable, you just want to use it in the code to achieve something. <code>strlen()<\/code> returns its value, allowing you to use it elsewhere.<\/p>\n<p>When you return a value with a function you would either use it right away (like in <code>strlen()<\/code>&#8216;s case), or assign it to a variable: <code>$short_text = get_excerpt( $content )<\/code>. You could then echo <code>$short_text<\/code> later when needed.<\/p>\n<p>If you want to learn more about functions (you should!), check out <a href=\"http:\/\/www.w3schools.com\/php\/php_functions.asp\" target=\"_blank\">W3Schools&#8217; tutorial on functions <\/a>.<\/p>\n<h2>Further Reading and Study<\/h2>\n<p>PHP is the basis for all themes and plugins so you&#8217;ll need to rely pretty heavily on this language. When working in a WordPress environment, you&#8217;ll use PHP functions (like <code>strlen<\/code> and <code>substr<\/code>), but there are also hundreds of functions created especially for WordPress by the core development team to help you. We&#8217;ve explored some of these functions in upcoming tutorials in this series.<\/p>\n<p>In this lesson, we&#8217;ve covered the basics you need to know to get started with WordPress development, but the truth is we&#8217;ve only just scratched the surface! It&#8217;s important that you read up on and explore each of the topics covered in this tutorial in more depth and practice, practice, practice!<\/p>\n<p><strong>Check back here next week for part three in our series, WordPress Development for Beginners: Building Themes.<\/strong><\/p>\n<p><strong>You can also read the first post in this series,\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/wordpress-development-beginners-getting-started\/\" target=\"_blank\">WordPress Development for Beginners: Getting Started<\/a>.<\/strong><\/p>\n<p><strong>But in the meantime,\u00a0check out the following resources to expand and cement your PHP knowledge:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.phptherightway.com\/\" target=\"_blank\">PHP The Right Way<\/a>\u00a0&#8211; A comprehensive and well-structured introduction to PHP if you\u2019re new to the language.<\/li>\n<li><a href=\"http:\/\/www.w3schools.com\/php\/\" target=\"_blank\">W3Schools<\/a>\u00a0&#8211;\u00a0W3Schools is a free web developers site, with tutorials and references on web development languages such as HTML, CSS, JavaScript, PHP, SQL, and jQuery, covering most aspects of web programming.<\/li>\n<li><a style=\"line-height: 1.5;\" href=\"http:\/\/www.tizag.com\/phpT\/\" target=\"_blank\">Tizag<\/a><span style=\"line-height: 1.5;\">\u00a0&#8211;\u00a0Tizag.com was created as a free stepping stone to the budding webmaster and a handy reference to the veteran developer. The site offers a tutorial that teaches the basics of PHP.<\/span><\/li>\n<li><a href=\"https:\/\/teamtreehouse.com\/\" target=\"_blank\">Treehouse<\/a>\u00a0&#8211; A premium site for learning to code that features over 1000 videos created by expert teachers on web design, coding, business, and more.<\/li>\n<li><a href=\"https:\/\/www.codecademy.com\/learn\/php\" target=\"_blank\">Codecademy<\/a>\u00a0&#8211; A free site for learning to interactively that features a great course on getting started with PHP.<\/li>\n<\/ul>\n<p><strong>Did you find this tutorial helpful? Why do you want to learn WordPress development? What do you want to know more about? Let us know in the comments below.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Becoming a WordPress developer goes hand-in-hand with learning PHP, the popular server-side scripting language with which WordPress is built. In this tutorial, you&#8217;ll learn the basic syntax of PHP, the logic behind how it works, and work through lots of coding examples.<\/p>\n","protected":false},"author":344049,"featured_media":151333,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[557,4161],"tags":[174,10076,9770],"tutorials_categories":[],"class_list":["post-149616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-wpmudev","tag-php","tag-beginners","tag-development-2"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=149616"}],"version-history":[{"count":26,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149616\/revisions"}],"predecessor-version":[{"id":217954,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149616\/revisions\/217954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/151333"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=149616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=149616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=149616"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=149616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}