October 4th, 2021 × #php#javascript#webdev
Hasty Treat - Spicy Takeout - PHP Is Good and We’re Just Re-Creating It
Wes and Scott discuss the merits of PHP and how modern JavaScript frameworks are reinventing many of its features like mixing logic and templates, hot reloading, and more.
- Intro to Spicy Takeout episode
- Mixing templates & logic frowned upon but makes sense
- PHP gets flack but has good features we now use in JS
- Every PHP request has own scope like serverless functions
- PHP is hot reloaded and serverless by default
- PHP has massive standard library that JS is catching up to
- URL based routing becoming popular again with JS frameworks
- Server-side rendering gaining popularity again
- Easy variable interpolation now possible in JS like PHP
- All-in-one frameworks like Laravel similar to past PHP frameworks
- Direct database access from templates possible but risky
- Why do people hate PHP and not use it nowadays?
- Using Laravel could be interesting to try nowadays
- PHP typing was half-baked but improving in PHP 8
- Inconsistent PHP API naming still an issue
- Why use JS over PHP nowadays despite PHP's merits?
- Final thoughts on PHP and its influence on JS
Transcript
Announcer
Monday. Monday. Monday.
Announcer
Open wide dev fans. Get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing, soft skill, web development, the hastiest, the craziest, the tastiest TS, web development treats coming in hot. Here is Wes, Barracuda, Boss, and Scott, El Toro Loco, Tolinski.
Intro to Spicy Takeout episode
Scott Tolinski
CSF. Welcome to Syntax.
Scott Tolinski
In this spicy takeout, we're gonna be talking about How the web has kind of just come back around to what we used to be doing with PHP and how we're just recreating most of it in JavaScript and What we mean by that and my name is Scott Talinski. I'm a developer from Denver, Colorado. And with me, as always, is the spicy Wes, badass boss.
Wes Bos
Spicy takeout. I was just, like, kinda, like, writing notes for this one. I was like, this would be a funny Segment. I don't know if we have a whole lot of spicy takeouts in us because Scott and I are not super opinionated. Yeah. We're pretty chill. Well, it's something that you're not necessarily super opinionated, but, Like, what's a spicy take that you got? Like, is there a is there a takeout that you could do? Well, I've been I've been getting pretty spicy on the React and Svelte stuff. That's true. I think we were trying to come up with names
Scott Tolinski
for what a segment like this could even be called.
Mixing templates & logic frowned upon but makes sense
Scott Tolinski
And, honestly, when you brought Spicy takeout to the table today. I was pretty shocked because that was great. It's very good. Yeah. It's perfect. I was joking that it's Dim Mak, which is the death touch.
Wes Bos
Alright. So we are sponsored by 2 awesome companies today. First 1 is FreshBooks, and second 1 is LogRocket.
Wes Bos
FreshBooks is The cloud accounting solution for your small business, for your freelance, for someone who's just making a little bit of money on the side No matter how big or small you are, I've been with FreshBooks for over 10 years now, and it's great because you can log all your invoices. You can send them off To all of your sponsors, funny. FreshBooks sponsors this podcast. And how do I build FreshBooks? With FreshBooks.
Wes Bos
It's crazy. So If you need some sort of cloud accounting solution, you can track hours and do expenses and all that stuff. Check it out. Freshbooks.comforward/ syntax. Make Sure you use syntax in the how did you hear about us section. Thank you, FreshBooks, for sponsoring. This episode is also sponsored by LogRocket,
Scott Tolinski
which is the perfect place to see all of your errors and exceptions happen. In fact, we just launched a new UI for our site, and that's pretty stressful, let me tell you. But One of the neat things about launching a new UI is having a tool like LogRocket be there as a helping support net To make sure that you can debug any issues that you're going to have, because when you rewrite your entire UI, you're gonna have some issues. There's no way around that. So we actually have LogRocket on our site, and we use it just about every single day to make sure that our users are taken care of whenever they run into issues on the site. Now what is LogRocket? It is a error and exception handling service that allows you to see a video scrubbable replay When you have an error, let's say a user has a problem with your site, it creates a scrubbable video replay, you can Get a scrubbable video that you can drag along and see exactly what happened, and you get the error log and network request and all that stuff that you use in your own development work environment.
Scott Tolinski
This is just one of the most amazing tools that I've seen, and you can attach users and all sorts of information to it. You can see how many errors that you have at any given time. It is really super cool. See it by browsers, all sorts of stuff. So check it out at logrocket.com Forward slash syntax, and you'll get 14 days for free. Again, this is one of those services you'll at least wanna go to their user dashboard and watch their little Their introduction, check out their home page. It's pretty amazing to see in action. Okay.
Scott Tolinski
So let's talk about spicy spicy takeout.
Scott Tolinski
Now, Wes, this is your baby here, so I want you to make sure that it's all represented correctly. So I'll let you kick this one off. Alright. So I've been having this
PHP gets flack but has good features we now use in JS
Wes Bos
thread in our notion for probably over a year now. And every now and then, I come back to it, and I'm like, you know, modern web development really is. A lot of it is we are just recreating a lot of the features that PHP has or has had for years years, and PHP gets a lot of flack, so this is my spicy takeout in that I'm gonna attempt to explain why PHP is not so bad And why it is that we and specifically, Scott and I, JavaScript developers are we're taking the best parts of PHP And recreating them in JavaScript land. So we'll go through all of the points that I have here about that as well as, well, I send a tweet out. It says, why do people hate PHP? So I'm not a PHP dev myself. I do sling it every now and then, but I am a big fan of it, and I'll I'll attempt to explain why in this This episode.
Scott Tolinski
It's been a long time since I've last wrote a line of PHP. I think probably, like, 2013, maybe. 2014. Wow. It's been that long for me. It's because it that not that I haven't written WordPress sites or done that, but I've mostly been doing them in a headless context. So, like, I'm not touching the PHP really. I'm adding Advanced custom fields and whatever. I'm tweaking those things, and maybe I've thrown a debugger in here or there. But, man, I I I really have not Really gotten into PHP. Alright. So first, just laughing. One of the comments on this is I heard Nickelback 7 is pretty good, which is what people I say about PHP 7. Anyway, first point here I thought they were okay. I get it now. That's a joke. Okay.
Every PHP request has own scope like serverless functions
Wes Bos
The first one is mixing template and logic. So one of the things people hated PHP for was separation of concerns, being like You can have your functions and all of your logic in the exact same file as your HTML that's rendered out. PHP is a templating language, but it also You could throw functions in there, and it's funny you sort of come back around, and you do that with JSX. You can put a function as well as render it out. And, Actually, I remember about 10 years ago now when I was at JSConf when React was announced, and everyone said, this is a joke. This is hilarious. Why would you miss mix all of your logic with your thing? And I remember it was really weird, but, like, a buddy of mine, we're in the hot tub, and it Just turned out the guy who did the talk was in the hot tub as well, and people are like, woah, rough rough talk. And he's like, yeah. Like, Facebook loves PHP, so this just makes So much sense for us. And and here we are, like, 10 years later, everyone's googoo gaga over mixing your templating and your logic all in one file, because sometimes it just makes a lot of sense, and it's really easy to do that. Yeah. There was definitely quite a bit of kerfuffle about that.
Scott Tolinski
And I remember never necessarily being as because I I do remember templates and logic in some projects that I had worked in in PHP, but, like, It always felt kind of natural for me to keep things together like that. And at the time, like, I was using Meteor, which did a lot of that too. So and I don't know. It didn't feel like that crazy to me or that scary at the time, but I understood why people were freaking out because, like, in programming, we hear these things being, you know, Repeat it over and over again, whether that's always have dry code or, you know, don't repeat yourself. We need that stuff, right, that you hear just infinite loop. And then all of a sudden, somebody does something slightly different to break one of those rules, and it's, like, shocked Pikachu face, you know, just, like, like, what are you doing? And it's it's not always bad to experiment what those rules mean exactly. Next one we have here is every request has its own scope. So this, I'm gonna loop this 1 in with
PHP is hot reloaded and serverless by default
Wes Bos
PHP.
Wes Bos
By default, it's hot reloaded and serverless. Mhmm. So the way that PHP works is this Invocation based, meaning that you write a script or you write a function, and then when somebody visits a URL where that function is called or that URL is Hit, then PHP starts. It invokes the script. It runs it, and it has its own scope for that specific function. There's no worrying about Sharing that between other people hitting that URL, and then when it's done, that script is done running, and it goes down. And that's kind of how serverless Is working as well. It doesn't have the scalability of serverless. You still have to think about scalability because you you can hit a a limit of running PHP scripts on a specific box, but the whole idea of hit a URL with serverless, spin up that function, Run the code and then spin down. That's exactly how PHP has has always worked, and there is no there's no starting a server or restarting it. And That's why I say it's hot reloading because, like, how do you test if something work with PHP? You refresh the page. And, or you refresh the script, and it will work. There's no starting and stopping of your servers, which is is kinda nifty.
PHP has massive standard library that JS is catching up to
Wes Bos
Next one, massive standard lib. So PSP, almost comically large. They have a standard live for pretty much everything you can think of. So need to format a date. You got it.
Wes Bos
Need to hash out a m d five. You got it. Need to resize an image. You got it. The audio audio bindings.
Wes Bos
So PHP is sort of the opposite of JavaScript in that there are bindings for absolutely everything, and It's pretty exhaustive when they go into now JavaScript, we're starting to see, like, things like the temporal API and a whole bunch of new method APIs, and We're starting to see JavaScript standard lib just grow and grow and grow in size where you don't necessarily have to reach for a
Scott Tolinski
Utility library for every single little thing that you need to do. Yeah. And that's, I think, a big plus. Yeah. The standard lib in JavaScript has been a little rough. Yeah. Naming conventions Of some of these are
Wes Bos
not amazing. That's one thing people have against PHP is that is it camel case? Does it have underscores? Is it just words just all mashed together? Like, I'm looking at the image API right now, and there's image color extract Alpha. It's all lowercase, all one word, and there's not a whole lot of consistency in that type of thing, which is pretty funny. I I always could see That is really frustrating. Yeah. You you would rely on your tools to know what those methods are that exist. Next one we have here is URL based routing. So by default, PHP, if you hit A script via the URL, then that script will run. And we're starting to see that now with a lot of these. Scott talked about this in SvelteKit. That's the way that it works is if you wanna have a dynamically generated what was it? A site map that you had? Mhmm. You just created a site map dot XML dot JS. And PHP land, you could very easily do the very same thing where you could dynamically generate any page via its URL.
Wes Bos
We see that in Next. Js and a lot of these new frameworks where you just hit the URL, and the script that is at that URL will run.
URL based routing becoming popular again with JS frameworks
Scott Tolinski
Wow.
Scott Tolinski
Yeah. Yeah.
Wes Bos
Starting to feel a little familiar. Yeah. I will say, like, there is also a bit of a Security concern there as well where you could potentially have something exposed that you don't want to have exposed. But And there's nuance here to all of this. So, like Totally. Totally. We're talking about the the broad strokes. What else? Server rendered. So server rendered everything is starting to become much more popular these days. What's the one in Ruby on Rails land that everyone's talking about? Hotwire? Hotwire.
Wes Bos
React is starting to roll out server rendered components. SvelteKit land has you can surf or render literally anything you want.
Server-side rendering gaining popularity again
Wes Bos
So people are starting to swing back the other way, Which is good. And that not everything needs to be client rendered. Yeah. Absolutely. What else do we got here? Get and post are just available. If you want your get and post input Variables to be available inside of a script, they are just available, and you don't have to pass them down from 8 levels low. You can get them wherever you want, and that we're starting See that in Next. Js Hooks land. What can I ask you what that looks like in SvelteKit land? How do you get access to those things? So you're getting access to the what is that actually? I don't even know what those are. If you were to submit a post request to your URL With, like, a form or if you were to have query params in a URL via those are Get. How would you access those in Sveltekit land? Oh, I mean, the the query params are just available inside of, like, a store that has all the location stuff in it. There you go. Yeah. Yeah. So it's avail like, a lot of that information about is available in in a given store or in the load function.
Wes Bos
Cool. Variable interpolation. You can just pop variables wherever you want. That's a big thing that we have in JavaScript land now, where in PHP, it's You have to use double quotes, and then you could just use dollar sign, the variable name.
Easy variable interpolation now possible in JS like PHP
Wes Bos
And recently in JavaScript, we have back text, and you can interpolate a variable Or if you want instead of having to use in both cases, in JavaScript, you have to use a plus to concatenate strings. And in PHP land, you have to use a dot To concatenate strings, and those are a pain either way. Yep. All in one frameworks that do absolutely everything they do. Your templating, they do your Auth, they do your database access, all those type of things. Those have been a thing a long time. Back when I was doing it, it was CakePHP, CodeIgniter.
Scott Tolinski
CodeIgniterium.
All-in-one frameworks like Laravel similar to past PHP frameworks
Wes Bos
Expression engine was of the paid version of that. Yeah. Last cup like, 6 years, probably, Laravel is the king of this. It's really nice to be able to do that, and then the Laravel community has a lot of really great frameworks and plug ins that you can use to And it's huge. Do things like Massive. Have a A GUI to edit your data. Yeah. Massive, massive community.
Direct database access from templates possible but risky
Wes Bos
DirectDB access. So it's not uncommon to see SQL statements directly inside of your templates, Which is something I believe we've talked about happening in JavaScript land at some point where you don't necessarily need a whole data layer. You could just reach into the database directly. Of course, that also has downsides, like SQL injections were extremely prevalent in PHP land. Most of those have now been cleaned up just because of the way. And, also, I should say, like, most apps are not directly SQL and data straight into their their views. They're using some sort of database
Scott Tolinski
access layer on top of that, but totally doable because the whole thing is server rendered. You don't necessarily have to worry about auth and all that. You can just run it. Yeah. You can run it on the server. There is something very nice about having To worry about 1 environment rather than the client and the server. Like, when you suddenly have to, like, this all this stuff has to run on the client and the server, you kinda inherently hit some Issues, whether that is security concerned about having access to that DB, whatever, but it does it makes it considerably more complex.
Wes Bos
So the question is, like, why do people hate PHP, and why are Scott and I not using PHP? Why do we still use JavaScript after
Why do people hate PHP and not use it nowadays?
Scott Tolinski
Saying all of these things to how amazing PHP is, why are we still going on in JavaScript? So what do you think? Why do people hate PHP Sky? You know, that's a Difficult question to answer because the answer really could be for a lot of reasons. People could hate PHP because they've heard somebody on Twitter say that they hate PHP. They could hate PHP because they wrote something in it, tried to modify a WordPress template, never really got super into it and had a hard time. Or they could hate PHP because they are a PHP master, and they understand everything about it and know all of its quirks and features that are no fun to work in sometimes. I did I mean, it could be any any number of reason why you would hate PHP. I don't I don't hate anything. I don't really hate PHP, but, yeah, People probably just hate it because of a bad experience that they've had with it at some point in their career. And whether that is modern PHP or not, You know, it could be any of those reasons,
Wes Bos
but I'm sure you might have some different insight. Yeah. I I think I agree with you there that It's a a combination of the 1st code they wrote that was awful was PHP. Yeah. The early web was very much built on WordPress, PHP, BB.
Wes Bos
A lot of these things that were had major security vulnerabilities pop up and they were sort of A bit of a mess to to work with. I think a lot of that has but there's still a lot of that in the world, but certainly there's a lot of awful JavaScript as well. Yeah. But a lot more modern PHP 7, those things are not as an issue anymore, and I think there's still a lot of people saying, yeah. I haven't touched it in 8 years, but it's awful. Right?
Using Laravel could be interesting to try nowadays
Scott Tolinski
Yeah. Right. Yeah. Yeah. I would be interested in building a site with Laravel just for fun to see, like, What does that feel like? What's that feel like in 2022
Wes Bos
moving on to the future future? I asked on Twitter what people said, and we got some good answers. It's blocking by default, so you have to get into threads if you wanna be able to not have blocking with JavaScript just to sync a weight.
Wes Bos
Beautiful. Yeah. Yeah. Absolutely. What else do we have here? Let me pull up a few more. A lot of people are saying the typing situation was sort of half baked, but with PHP 8, that's gonna be changed. So Okay. I was Just googling that.
PHP typing was half-baked but improving in PHP 8
Wes Bos
There you go.
Wes Bos
What is the type situation in PHP? Yeah. I think you can type a lot of things in PHP, but, You can go, like, fully typed in PHP 8, which are pretty good. A lot of people saying the inconsistent API naming, that Would definitely bug me as well Yep. Because they haven't tried Laravel yet. People the Laravel devs are, like, some of the coolest people I know, I think, because they're just like, Yeah. Whatever. I like PHP, and they're not, like, trying to, like, convince you otherwise. They're just like, yeah. It's sweet. I enjoy it, And we run our app on it. It works well. So that always has looked good in my eyes, where the Laravel people are never obnoxious about using Laravel. They're just like, yeah. Sweet. Whatever.
Scott Tolinski
Yeah. We're building this wicked business. I'm efficient in it. I, make stuff in it. Yeah. You see that all the time. People there is a huge community out there of people making Awesome stuff with it. So I gotta have my I gotta have my client side JS, man. I gotta have that. Yeah. So there we go. Let's move to the next one. Why are we not using p if If we are so spicy on PHP being decent, like, why is JavaScript still better? I love how that's like a spicy take.
Why use JS over PHP nowadays despite PHP's merits?
Wes Bos
So sharing code between front end and back end, not having a context switch. Like, at the end of the day, you will have to write JavaScript for your application because, honestly, that's all you got. That's all you got. Sorry about that, but you can't run PHP in the browser. So if that's the case, then 1 single language to rule them all, being able to share dependencies from the back end to the front end. Honestly, the ability to share Server render component, but then also rerender that sucker
Scott Tolinski
on the client is really good. Client side rendering or in client side routing is great For me, I love client side routing, not having to Totally. Rerender the entire page. Every single attempt of going there, you can, you know, only Swap out segments of your page really easily. You can get page transitions and animations, those types of things. We share types between the server and client, And then, I mean, I guess, some of that even comes from our API being in GraphQL, but we have physical
Wes Bos
types files that we can share between the 2 of them, and that that's really huge. Totally. At the end of the day, I I think I just like the JavaScript syntax better myself. I'm better at it. Yeah. And that's Probably the thing at the other day is I am a much better JavaScript developer than I am a PHP developer, so there's something for that to be said. We have an entire show. If you go back to 267, That's something we haven't touched on, and it's the server generated HTML, JavaScript sprinkles, turbo links. So that sorta that's the, like, PHP developers answer to a lot of this stuff is you can server render it and then pick it up in the browser or with things like Turbolinks Or stimulus, Alpine JS. I saw Vue is also rolling out like a version of Vue where you can just use The view bindings in your HTML Mhmm. And it will very much like Angular was in the in the early days. So there's there's a whole section of that as well. It just I just need to know that everyone's app is different, and there's a lot of different use cases for it. So that's my thoughts on PHP.
Final thoughts on PHP and its influence on JS
Wes Bos
Hopefully, it wasn't too spicy for you, but I think that we've learned a lot from PHP. I have some extra spice to bring to this conversation. Yes. Let's have it. If Hotwire
Scott Tolinski
was really great. Like and hey.com was like or, hey, the emails.
Scott Tolinski
Was there gonna be their big thing for using Hotwire? Like, shouldn't hate perform better? I haven't used it in a little while. Maybe that's a thing, but, like, hate was never super performant for me. And so maybe that kinda turned me off of the idea of Hotwire, and that could be just, like, straight up ignorance speaking. So if you wanna correct me, come at me. But I always felt like This is kind of a weird product to promote this new thing to do on because it it wasn't exactly like, you know, this is the fastest project I've ever used in my entire life or something. It always felt kinda slow, and I granted emails
Wes Bos
email's tough. But Yeah. Interesting. I only use hey, like, the 1st day, and then I Stopped using it after my name expired on it, and I wasn't super impressed with it, but it's kind of cool that they are what they're trying to do. I like the idea And the the ideas behind what they're doing much more than actually having to switch my email over. Yes. It kinda felt like, hey. What if what if you used a Google inbox but Had to have an entirely new email for it. I was like, yeah. I don't know about that. Yeah. Alright. That's it for today. Thanks for tuning in. If you have any Spicy takes that you think we should maybe try to cover? Tweet us at syntax f m, and, we'll see what we can do for you. Zig. Peace. Peace.
Scott Tolinski
Head on over to syntax.fm for a full archive of all of our shows, and don't forget to subscribe in your CAS player, or drop a review if you like this show.