 Welcome. Thank you for joining me today. So we're going to talk a bit about PHP 7. Is anybody excited about PHP 7? Yay, great. All right. So there is some code on my slides, and some of it gets a little small. If you want to pull up my slides, there's a link right there. And that link will be right back. But my name is Wilka. I'm a software engineer at AdDis, and I'm the lead engineer on our WordPress plugins. We do a bunch of integrations to make it easier for website owners to use our tools. And WordPress is by far our most successful integration. And we do a lot of work to make sure it will run in as many environments as we can, whether that be different versions of WordPress or options to troubleshoot with things that might be doing things a little oddly, and also PHP versions. So PHP 7 is exciting. There is new features in there. But if you don't get to use those new features for reasons like backwards compatibility, there's still some incentive to switch over to PHP 7, and that's performance. It's a lot faster. There's some impressive benchmarks out there, showing the same code running on PHP 7 two to four times as fast as it does on PHP 5. And with how much of the internet is generated by PHP, this has a cumulative possibility of really speeding up the internet for us. But PHP 7 has breaking changes. Some of the things we do in PHP 5 just don't work in PHP 7, and it'll throw errors. Other things that we do in PHP 5 work differently in PHP 7, and so they might not do what we expect, and they might break our themes or our plugins. PHP 7 is now the recommended version of PHP for WordPress, and that means we'll be seeing more and more versions of WordPress on PHP 7, more instances on PHP 7. There is a flip side to that, and that's that not everyone will upgrade. Not all new environments will come on with PHP 7, and we'll still have a lot of older versions of PHP to support. WordPress supports all the way back to 5.2.4. That's a magic number. 5.2.4 was released in August of 2007. It's coming up on its 10th birthday. It's a lot of years of PHP for us to be supporting. This is a graph from last night off of WordPress.org, showing the different versions of PHP that they see WordPress sites running on. That biggest sliver on there, that big red one, at 39.9%, that's PHP 5.6. But different slices of this graph represent everything from 5.2 all the way through 7.1. That 7.0 version there that's highlighted at 9.6%. Last week, that was at 9.4, and a couple of weeks earlier, that was at 9. So that is going up, and we are seeing more of these sites. But there are a lot of WordPress sites out there, as we all know. Even that small sliver up top, the 4.8%, that's PHP 5.2, that's a lot of WordPress sites out there that might want to run your code. If you are supporting a variety of customers or working on a public theme or a plugin, you might want to consider widening the versions of PHP that you support. All right, so the first step in this, I'm sorry, is testing. We can never get away from that testing. I'm going to talk to you about a bunch of things today you can look at when you're writing your code and things you can use to make the process smoother when you're testing to make sure that your code works and all the environments that you care about. But if you're not testing your code and both PHP 5 and PHP 7, you can't be sure that it's doing what you want. So that testing still needs to happen. But let's get into some of those things that you can be doing now, other than testing. So today we're going to talk about uniform variable syntax. It's the new way that, it's the new way PHP decides how to evaluate dynamic variables. We're going to talk about some changes in for each, some removed functions, a changed function, a new rule around switch statements, some new, a new rule about function parameters, changes in error handling in PHP 7, and a little bit about PHP 4 style constructors. So let's jump right into uniform variable syntax. So when we use other variables to create the names of variables we want to look up in PHP, in PHP 5 we have a bunch of rules to memorize on what gets evaluated first. In PHP 7 they're aiming to make this easier with a left to right evaluation. We take the smallest part on the far left that we can evaluate and we work off of that and move right. So that's different than PHP 5. Let's jump into some examples. So let's go through this one first and how it would be evaluated in PHP 5. We'd start off with dollar sign through and then we'd take that bar, so we'd treat dollar sign through as an associate of array and we'd look up the key bar and grab the value from that, then we'd treat that as another associate of array and grab the key baz, the value at that. So let's say at this point we have a string with the text pink. So at this point we have the string pink. We put that dollar sign back at the front and now we are looking for a variable called dollar sign pink. It's really round the way of getting to that. Here's PHP 7. We start with dollar sign, dollar sign through. So we'll look at dollar sign through and say in this instance that it's a string with the text orange. So here we're looking for a variable called dollar sign orange and we're treating it as an associate of array. We're gonna look up the key bar and we're gonna take the value from that and treat that as another associate of array. And from that take the value at the key baz. And that's what we end up with. So here's that difference illustrated side by side with some curly brackets, the orange ones. As you can see this gets evaluated differently in PHP 5 and PHP 7. So how do we write code that can do this in both places? Well, the answer is those curly brackets. We add those in. If you're writing PHP 5 code and your code is doing what you want it to, and you want it to evaluate the same way in PHP 7 for this statement, you take that first line and use that in your code with those curly brackets. And PHP 7 will understand that it needs to evaluate everything inside the curly brackets before it goes outside of them. And the same way if you're developing in PHP 7 and you wanna make sure that this variable gets evaluated in the same way in PHP 5, you use the curly brackets on the second line and tell PHP 5 that dollar sign, dollar sign food needs to be evaluated together. And then it'll go on to the rest. Let's go through one more example like that. So the way this would evaluate in PHP 5. We start off with that dollar sign bar and treat that as an associative array. Look up the key, baz, and grab the value from that. Let's say that that is the string green. And then we go back and we grab dollar sign food, treat it as an object, and we are gonna be looking up a property on dollar sign food that is called green. So we're looking for dollar sign food, arrows, green. From there, we treat it as a function and we execute it. Here's what happens in PHP 7. We start with dollar sign food. We treat it as an object and then we go to dollar sign bar. Let's say dollar sign bar is a string blue. So now we're looking up the property blue in the object dollar sign food. From here, we treat that property blue as an associative array. We'd look up the key, baz, in there, grab the value, and then treat it as a function and execute that function. Again, very different from PHP 5 and PHP 7. So if you're writing PHP 5 code and this code does what you want it to without those curly brackets, you want it to behave the same way in PHP 7. You'd use that first line with the curly brackets and PHP 7 will know what parts to evaluate how. And if you're writing in PHP 7 and everything's working the way you want it, you can add curly brackets in the bottom part and then PHP 5 will know to evaluate it that way as well. There's another change in variables in PHP 7 and that's globals. So in PHP 7, we can't say global dollar sign dollar sign. In PHP 5, it's perfectly valid for us to say this, global dollar sign dollar sign food, arrow bar. But in PHP 7, this will throw a fatal error. And that's because of those double dollar signs. The solution here is really simple and it's more of those curly brackets. With this, PHP 7 will be happy and this will work in PHP 5 and PHP 7. All right, there's a couple changes in for each when we iterate over arrays. And the biggest one is that for each doesn't change the array's internal array pointer anymore. An array pointer is a place that PHP tracks of what it should look at next in that array. Not everything's going to use that array pointer, but some things do. In PHP 5, that array pointer changes as we do it for each over a function. In PHP 7, it doesn't. To look for places in your code that might be depending on that array pointer, I suggest looking through your code base for uses of the current function or the reset function. Those are the ones I use the most often that manipulate that pointer. And that'll give you a heads up of places where you might need to rewrite your code and logic to not depend on that pointer changing in it for each. There's also a change in for each by value. So when we do it for each by value, we'll say for each dollar sign array as dollar sign value. In PHP 7, what happens behind the scenes is that it takes dollar sign array and it makes a copy of it, its own copy that you don't use directly. And it will iterate through that copy of dollar sign array. It doesn't actually iterate through dollar sign array itself. What this means is if we add more, if within our for each loop, we add in more items onto that array, we're not going to end up looping through those items because we're actually working on a copy of dollar sign array, not it itself, and that copy hasn't changed. This also means that if you're doing things in dollar sign in your for each loop that are looking ahead of where you are now and making changes in dollar sign array for things that you'll loop through later, those won't be available to you in dollar sign value, those changes, because you're actually working off of a copy again and not dollar sign array itself. For looking ahead, you can sort of get around this by asking for the key as well. So dollar sign array as dollar sign key arrow value. And then you can use that key to grab the value that you need if it might have been changed other times. If you need to add things onto your for each, then we can do and still loop over them, we can do a for each by reference. So for a for each by reference, this has changed a bit in PHP 7 and the change is that you can now iterate over things you add to the array as you're going. You couldn't do that in PHP 5. So for each by reference, what we do is we say for each dollar sign array as ampersand dollar sign value. That ampersand is the reference. What that means is when we're in that for each loop that dollar sign value value is actually the thing that is in dollar sign array. And if we change something in dollar sign value, it's going to change in the array as well. So be careful with this one. This one would solve an issue if you need to add things onto your array and iterate over them as well. But be careful with any changes you do to dollar sign value because those changes are gonna stick around. You don't wanna do anything destructive to setting to values in there that you might care about. All right, remove functions. So functions deprecated in PHP 5 have been removed from PHP 7. The eREG extension for PHP was deprecated in 5.3 and that is now gone for PHP 7. It's not available, but you can use PCRE instead. The MySQL extension was deprecated in 5.5 and is no longer available for PHP 7, but MySQL I is a drop in replacement extension. That means that you should be able to simply change over those MySQL functions to say MySQL I instead of MySQL and they should work right out of the box. All right, split. This function was deprecated in 5.3 and it is now gone in 7. Split would take three parameters. It would take a pattern, which could be just a string or a regular expression as its first parameter. Its second parameter would be a string you want to search through and then optionally you could give it a limit. Negative one meant unlimited, but you could say five and only get five results. This would look through string for your pattern and break that string up into an array and return you an array of those different parts. So if you give it a paragraph of text as your string and a space as your pattern, it would break it up by word and give you back an array with strings for each word in the paragraph. We can't use this in PHP 7, but there are alternatives we can use instead. So option one that I would suggest is the function explode. There's a caveat. This function only works for you if that pattern you're using is a string and not a regular expression. It has a very similar signature. It'll take that pattern as your first parameter and your string as a second and then a limit. Its limit works a little differently though. If you're giving a positive limit, you don't need to worry about the changes. It works just the same. And if you're leaving that limit off, it works just the same. Leaving that limit off will give you unlimited results, however many come out of that string. But wherein split a limit of negative one meant unlimited and explode a limit of negative one means chop off my last result. Negative two would mean chop off my last two results. So if you're using that limit and putting a negative one in there for split, you'll want to change that a little for explode. Another option is preg split. And this one is basically a drop in replacement. You can use this for both strings and regular expressions, but it will be a little slower than explode for you if you're just using a string. And it is a drop in replacement. We've got the same first three parameters, pattern, string, and then limit. The limit behaves the same way and then we have an extra parameter that would change the way the regular expression is evaluated. All right, changed functions. I want to talk to you about list. There's two changes to how list works in PHP 7. So list is a little weird and the syntax isn't your general assigning variables values syntax for PHP. So the first item here, for example, says list $a, $c, $d, and then equals an array with some strings. What list will do is it'll take the strings in the array or any values that you have in those array and populate them into those variables that you put in as parameters to list. So b, c, and d. b will become the string apple, c will become the string banana, d will become the string carrots, carrot. Now the difference for PHP 5 versus PHP 7 is the order in which it assigns those. If you're using it like this first example here, you don't need to worry about this, but in PHP 5, what happens when PHP comes across this list is it goes to $d and it puts in the value carrot. Then it goes to $c and puts in the value banana. Then it moves on to $b and puts in the string apple. In PHP 7, that's reversed. So first it'll put apple into $b, then it'll put banana into $c and then carrot into $d. Now when we're using it this way, that doesn't matter to us. Everything works in the same, that order isn't going to affect us, but if we're doing something fancier with list, it might. So in the second line here of code, instead of putting those values into a variable, I'm pushing them onto the end of an array. So I have a $a brackets, $a brackets, $a brackets. And with this, the array that I get in dollar sign array is different from PHP 5 to PHP 7. So in PHP 5, I would get carrot banana apple. In PHP 7, I get apple banana carrot. All right, there's also another use for list in PHP 5 that is gone in PHP 7. Not even sure how well documented this is, but it does get used in some places. And this is a weird use of it. So you could put a string into a variable. So say we have dollar sign string and we put in the string hello. We could then say list dollar sign b equals that string and what list would do would be to explode that into an array with one character per item in the array. So we end up with an h and e, another l, another l and another o. Now this was tricky in PHP 5 because you couldn't say list dollar sign b equals quotes hello. That wouldn't work. You had to put it in an actual variable and then it would work for you. But we can't do this in PHP 7. An alternative for this is string underscore split, str underscore split, and this will give us the same result though it is a more common format for assigning. So if we wanted to make dollar sign, if we wanted to put that array in dollar sign b, we would say dollar sign b equals string split and then the string hello. All right, switch statements. There's a new rule in PHP 7 for switch statements and that's that you can't have multiple defaults. PHP 5 didn't care. You could put as many defaults in there as you wanted. It would ignore all of them but the last one. PHP 7 will throw you a fatal error. But the fix for this is pretty straightforward because now you know that it only uses that last one. If you run into this you can just delete all the earlier ones and you should be good. All right, function parameters. There's a new rule for those in PHP 7. Functions cannot have multiple parameters with the same name. PHP 5 didn't mind this. You could write a function foo that took two parameters, dollar sign bar and dollar sign bar and everything would run fine. You'd get the second value passed into that function for dollar sign bar but it wouldn't complain to you about it. PHP 7 will actually throw an e-compile error if you do this. Again, the fix for this is pretty straightforward. Either you can clean up your code to not reuse the same variable name but if you're trying to match the signature of another function, you could just rename that first one to something you don't use. So I could say dollar sign bar one, dollar sign bar and then leave the rest of my function as it was because that second one was the one being used. All right, error handling. There's a lot of changes in error handling in PHP 7 and that's primarily that they mostly throw exceptions now instead of errors, including fatal and recoverable errors. What this means is that if you come across a fatal and recoverable error, you could do a try catch, catch that and then continue running more code. There's a lot of care put into this to make it backwards compatible in PHP 7 but I do know of one instance where it's not and that's if you use a set error handler function. So the set error handler function takes a parameter of another function and it'll execute that function whenever it gets an error. So you could use that to maybe log those errors to a database or do something special with those errors. The difference here is in PHP 7, you're not going to get all the same errors caught by this as you did before because some of them are going to come out as exceptions. So if you're actually using set error handler, you'll want to look a lot more into the changes in PHP and error handling. All right, PHP 4 style constructors. This is the one thing on here that I'm talking about that's just deprecated and not yet removed. In PHP 4, when we created a class, we would create our constructor with the same name as a class if we wanted a constructor. So class foo would have a function also called foo and that function would be our constructor. If we said new foo, that function foo would execute for us. This is deprecated in PHP 7, but it's still around. So if you're using it, it'll throw some notices for you, but it shouldn't break your code. In PHP 5, they introduced this constructor instead, underscore underscore construct. And so here, if we asked for a new foo, that function underscore underscore construct would be executed. This is completely valid in all the versions of PHP. So what PHP 5 would do here is it would use, PHP 5 and 7 would use underscore underscore construct as a constructor and ignore function foo. You could call that in another way if you want to use that somewhere. All right, so we've talked about uniform variable syntax and how it makes it easier for us to understand how our dynamic variable names are going to get evaluated in PHP 7. We talked about three different changes for each. Some removed functions, a changed function, new rule for switch statements, you can only use one default. A new rule for function parameters in PHP 7, you can't reuse the same variable names for your parameters. Error handling and how a lot more of it is now exceptions that you can catch, including some fatal and recoverable ones, and PHP 4 style constructors. I think this is also a good time for us to talk a little bit about PHP documentation. This is a screenshot from php.net. I use this constantly when I am writing code, even for functions that I'm very confident I know how to use. It takes an extra 30 seconds and I can double check what versions of PHP it's supported in, as well as check the in and out and make sure that I'm using it as intended. So let's look at the documentation for a couple of the functions that we talked about. Here's a documentation for split. And over here, we can see that split is available to us in PHP 4 and PHP 5 and notably not PHP 7. We also have a warning down here letting us know it was deprecated in 5.3.0 and removed in 7.0.0. It even goes on to suggest different functions we could use in its place. Here's the documentation for list and here are the PHP versions. So we've got PHP 4, PHP 5, PHP 7. We're good. We get to use this one. If we scroll down further, we also have a warning that tells us about the difference in what the order that values get assigned in PHP 5 versus PHP 7. It's right there for us to look at anytime we want to use list. All right, here's a more complicated one. Array replaced recursive. So here we see that it's available in PHP 5 and PHP 7. But it's available in PHP 5 for versions 5.3.0 and newer. That magic version for us with WordPress is 5.2.4. That means that there are WordPress sites out there up to date with the latest versions of WordPress that can't use this function. And that would be the versions that are 5.2. That's about 4.8% of WordPress installs. Now usually when I run into this, I'll make an extra effort to find a different way of doing what I need to do in a way that will work across all the versions of PHP I need to support. And I can usually find pretty simple ways of doing that. Though sometimes they're a little less elegant and there may be some extra steps. This function is actually an example of one that I use anyway. And here's how I get away with that. So there's a place early on in our plugin where I check to see if array replace recursive exists. If it exists, then I'm good. I can go on and use it. And that means that I'm either in a version of PHP that's 5.3.0 or newer, or someone else has already come in and polyfilled it for me. If it doesn't exist, then I go ahead and define it. I could probably do a better job of defining it than I have here because I only take two parameters and the real one can take any number of parameters, but this works for my purposes. All right. I also wanted to share with you some more resources for looking at different changes in PHP. I talked to you about the ones I've seen the most, but these articles will talk to you about migration and incompatible things between five and seven. The next one talks about the deprecated functions in PHP 7 that have been removed, the deprecated in PHP 5 removed in PHP 7, a bit about PHP 4 constructors, and then the last one is about things deprecated in PHP 7. All right, so I hope I've left you in a better position to get your code working great in all the 10 years of PHP that WordPress supports. Thank you for joining me today. Thank you very much indeed, Yolker. That was an extraordinary amount of information to share in a pretty compressed time period. I'm sure there are opportunities for further deep dive right now. Are you going to be around maybe over the expert bar after this? Okay, will that be an opportunity to probably deep dive as well. But while we've got you here, are there questions from the floor around today's discussion that we'd like to follow up with right now? If you have got a question from the floor, the microphones are just about eight rows back under these aisles in the Arctic zone over there, or the slightly warmer half over there. It would appear we do not. Okay, okay, well now this is a real challenge for me because I thought what I would do is, I could attribute a question. I'm not a developer. Okay, but here's a question. There is a question I guess, which is like if you're not necessarily a developer, if PHP isn't a part of WordPress that you would necessarily get to on a day-to-day basis, are there areas of the differences moving to PHP 7 that someone can as a non-developer sort of keep an eye out for, at science look for or things that you, are there classes of plugins maybe that you think could be problematic, something for the non-developer community? So I don't remember the name of it, but there is a plugin that will actually scan your plugins to let you know whether or not it finds anything that might be a problem in PHP 7. I can look that up and tweet it out later. That would be awesome. It's not foolproof, but it does go in and check for, search through the code for things that are likely to be problematic and let you know about those before you upgrade. Okay, and what's the general sort of spread of PHP 7 across most of the hosts that people will be using these days? Are there any sort of dark areas where it's still, Not that I know of. Okay. We haven't had much problems with it. Okay, Johnny, good.