• About
  • Write For Us
  • Contact
  • Top 50 Photography Blogs
  • Resources
  • Forum

IndustryDev

  • Design
    • Photoshop
    • Lightroom
    • Camera Raw
    • Bridge
  • Development
    • HTML
    • CSS
    • Javascript
    • PHP
    • Dev Tools
    • WordPress
  • Photography
  • Blogging
  • Technology
  • Inspiration
You are here: Home / PHP / Internal PHP Functions

Internal PHP Functions

May 13, 2015

Internal PHP Functions

I am currently embarking in the final stage of Hampton Paulk’s “PHP Functions” course at Treehouse. It’s been a good one so far. I’ve learned a lot. Hampton has a way of feeding information just slow enough as to be easily absorbed.

In this post, I’m going to go over functions that are internal, or built in, to PHP. Some of these functions come standard with PHP (core functions) and some need to have PHP extensions compiled in.

Function Documentation

The most important area you need to concern yourself with when dealing with internal PHP functions is where to find them and how to parse through the documentation. In this section, we’ll go over exactly how to go about doing that.

Finding Functions

So, where exactly can you find PHP’s internal functions? Well, if you follow this link, you’ll see the “home base” for this area on the PHP site. If you’d like to browse through all the internal function categories on the PHP site, you can visit the Function Reference section.

Reading Functions

On the same page, you’ll see a link that says, ” how to read a function definition” and if you click it, you’ll end up at the “How to read a function definition (prototype)” page. This is where you’ll find the explanation of how exactly to pick apart each area of an internal function.

On this page, you get a pretty good breakdown of a function. The PHP manual explains the function name, the history of the function, what the function returns and parameters/arguments for the function. Although the PHP manual uses only the strlen() function for this example, you can use this as a good guide and apply the same principles across all internal functions.

String Functions

One of the more popular sections of all internal functions are string functions. While this section is widely used, Hampton is going over some of the most popular.


strlen

The first function we’re going to look at is the strlen function. This returns the length of the given string. Let’s go over some code.

<?php
$phrase = "This is my first sentence.";
$len = strlen($phrase);
echo $len;
?>

If we look at the above code, we can see that I created a string and set it equal to a variable called $phrase. Then, I used the strlen function and passed through the $phrase variable as an argument. Since that doesn’t output anything to the screen, I set the strlen function to a variable called $len. Finally, I echoed out the $len variable. For this code, the output is:

26


substr

The next function we’re going to look at is called substr. This function returns part of a string. Let’s look at some code.

<?php
$phrase = "This is my first sentence.";
echo substr($phrase, 0);
?>

If we look at the above code, we can see that I kept the initial string and variable. I changed the other areas though. In the case above, I used the substr function to return the entire string. The way I did this was to pass the $phrase variable to the substr function and then begin the output at the first letter. That’s what the “0” is. It’s the first position in the string. Then, I echoed out the function. In this case, the output is:

This is my first sentence.

Take a look at this next example:

<?php
$phrase = "This is my first sentence.";
echo substr($phrase, 5);
?>

In this example, I changed the 0 argument to 5 and now the string is returned beginning at the fifth character:

is my first sentence.

While the previous example began at the fifth character and continued on to output the remaining characters in the string, this next example begins at the first character again and then only outputs the next five. Here’s the code for that:

<?php
$phrase = "This is my first sentence.";
echo substr($phrase, 0, 5);
?>

The output for this example is:

This

Notice that the fifth character is a space and the rest of the string is truncated.

strpos


The next function we’re going to look at the called strpos. This function finds the position of the first occurrence of a substring in a string. Let’s look at some code.

<?php
$phrase = "This is my first sentence.";
echo strpos($phrase, "first");
?>

In the above code, you can see that I, again, kept the first line the same. After that, I used the strpos function and passed into it the $phrase variable. I also passed part of the string into the function. This part is the word, “first.” Basically, I want to see what character position (in the zero based index) the word “first” resides. In this case, the output is:

11

This means that the first letter in the word “first” resides at the eleventh character position in the entire string $phrase.

Let’s look at one final example that combines the two previous string functions.

<?php
$phrase = "This is my first sentence.";
$start = strpos($phrase, "first");
echo substr($phrase, $start);
?>

Now, don’t let this one confuse you. All we’re really doing here is setting different values to variables and then using those variables to get the output we want. The way to think of this is that we’re making something “equal” to something else.

Again, I kept the first line the same. That’s our string. On the second line of the code, I wanted to find the beginning position of the word “first.” We just went over this. The position is 11. I set that output to a variable called $start. Just with these two lines, we have two variables that “equal” some data. Here they are:

$phrase = This is my first sentence.


$start = 11

Now that we know what the string is and we know what the position of the word “first” is, we can do something with it. In this case, we want to output our string at position 11, or wherever the word “first” starts. The way to do this is to use the substr function we went over above and pass to it the $phrase variable. That’s easy – we just did that above. So, if we left things just like that, our output would be the entire string. We want to output the string beginning at position 11 though, and since we have a variable that’s equal to that position, we simple use that variable as our second argument. So, as the second argument, we insert $start. In this case, our output is:

first sentence.

How cool is that?

Array Functions

The next section of internal PHP functions we’re going to look at is array functions. And more specifically, we’ll be going over the array_keys function and the array_walk function.

array_keys

The array_keys function returns all the keys or a subset of the keys of an array. What the heck does that mean? Let’s take a look at some code to get a clearer picture of what this function can do.

<?php
$names = array(
	"Jay" => "Man",
	"Rob" => "Boy",
	"Russell" => "Manboy"
);
var_dump(array_keys($names));
?>

If we create an array called $names and then var_dump that array using the array_keys function, we get this output:

array(3) { [0]=> string(3) “Jay” [1]=> string(3) “Rob” [2]=> string(7) “Russell” }


This doesn’t mean all too much to us, but if you pick apart the output, you can see that we have three values in our array. For each key, in the square brackets, we get the type of value and the length of the value.

If we want to loop through each key and output a message, we can do so with this code:

<?php
$names = array(
	"Jay" => "Man",
	"Rob" => "Boy",
	"Russell" => "Manboy"
);
foreach (array_keys($names) as $name) {
	echo "Hello $name<br>";
}
?>

This will give us the output:

Hello Jay
Hello Rob
Hello Russell

array_walk

The next function we’re going to go over is the array_walk function. This function applies a user supplied function to every member of an array. Again, we don’t necessarily know what that means, so we’ll look more closely through some code examples.

<?php
$names = array(
	"Jay" => "Man",
	"Rob" => "Boy",
	"Russell" => "Manboy"
);
function print_info($value, $key) {
	echo "$key is a $value.<br>";
}
array_walk($names, print_info);
?>

In the code example above, we’re using the same array as above. For the second code block, I created a function called “print_info” and passed through, two values. The first is a variable called $value and the second is a variable called $key. Inside the function, we simply echo out a sentence that says, $key is a $value.

In order to pass through those two variables, we’ll use the array_walk function to go through each key/value pair one at a time. Inside the array_walk function, we’ll pass through our array called $names and the our callback, which is the print_info function itself. We’ll get this output:


Jay is a Man.
Rob is a Boy.
Russell is a Manboy.

I’m not going to lie to you here when I say this one is a bit confusing. I’m still trying to wrap my head around the $key variable. I’m not sure if that’s something that’s internal to PHP and this function. I’m also not sure how it was created. I’ll have to look into it more. If you have any thoughts, please let me know in the comment section below. From that I’m gathering from other sites, it seems like it’s just “there.”

Well, that brings us to the end of Hampton’s “PHP Functions” course on Treehouse. I learned a lot here and really gained an understanding of it by writing it all down. I’m sure that last part about the $key variable will gnaw at me for some time until I figure it out, but otherwise, great course.

If you have any questions about this post, please ask in the comment section below. If you would like to check out some of my other PHP posts, please click the PHP link at the top of this page.

Related posts:

  1. PHP Function Returns
  2. Operators in PHP
  3. PHP Data Types
  4. Conditional Statements & Loops in PHP
  5. PHP Variables, Statements, Comments & Whitespace

Filed Under: PHP

What’s Next? Email Updates!

If you enjoyed reading this post, why not consider signing up to receive others like it by email? It's so easy and you can unsubscribe at any time.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Connect With Me

  • Facebook
  • Instagram
  • Pinterest
  • RSS
  • Twitter

Copyright © 2021 · IndustryDev.com Web Design & Photography Blog · About · Write For Us · Contact · Privacy / Terms · Sitemap