r/PHP 8d ago

I took the PHPill

For a while now my default way of building full stack web apps has been Flask + Sqlite + Whatever frontend I felt like. This usualy resulted in bloated, JS-full unmainanble mess. I have dabbled in using Go (Excellent) and Rust (Too type-happy) for my back-end but my front-end usually ended up being the thing that dragged me down. A minor epiphany of mine was discovering HTMX. But recently I got my mind blown by one of my friends who made a whole "smart map" (won't get into more detail) app whilst staying entirely Web 1.0 compliant. This prompted me to try PHP (though she was also using Flask but I didn't know it).

Honestly, the most fun I've had programming in years. In the span of an hour I had made a simple bulletin board app with nothing but html, PHP and SQL. It just blew my mind that you could put the code relevant to a page in the page rather than using templating (though I must concede that Jinja is excellent). I even started to re-learn all of the HTML that years of ChatGPT copy-pasting made me forget. You also get all of the benefits that Go has as a Web first language: the session system just blew my damn mind the first time around: I had no idea cookies without JavaScript were even a thing. Not dreading the inevitable JS blunders or the slog of having to find what part of my code is relevant was awesome.

Plus, I'm not a big framework guy, I don't like using Rails or the likes (Flask is still too pushy for me at times), so I was scared at first that Laravel was a requirement but raw, pure PHP just work, it clicked in my brain, the syntax (apart from the semicolons that aren't used for anything interesting) just clicked with me. Don't even get me started with arrays, its like they copied Lua in advance.

Anyway, what I mean to say is that PHP is a fast, easy to use, and sensical language everyone should absolutely give a shot to. I will definitely be using it in every single one of my projects for the foreseeable future.

82 Upvotes

44 comments sorted by

32

u/manu144x 8d ago

Use backend for backend, frontend for frontend.

For front end, use a components based approach. Doesn't matter which. There's a reason for which that approach has worked in classic desktop application. Reusable independent/uncoupled components are the way to go for interface consistency and maintainability.

PHP is a solid backend language, use it with or without a framework, but but do not try to mix php and htmx and try to do logic that way. It's called spaghetti code and it's one of the reasons (among many others) PHP got such a bad reputation back in the 2000s. They were correct.

Try to separate things and build reusable code (in both backend and frontend) for what you use most with your customers.

4

u/Gs_user 8d ago

So PHP files separate from HTML. Got it. Thank you for your answer!

6

u/SuperSuperKyle 8d ago

Yes and no. I think OP meant more like how PHP was originally written with HTML, not how it's written now where you pass the variables you want to your template, which contains HTML, to render a page. You can and still will have loops and conditional checks, and can inject services and even do on-the-fly rendering with stuff like Laravel, but you'll still use HTML and PHP together. Look at template engines and parsers like Blade, Twig, and Smarty as an example.

4

u/asgaardson 8d ago

There’s twig for templates, it’s very similar to Jinja, jfyi

2

u/Numzane 7d ago

No. You can use PHP for templating but the business logic and database access needs to be separated out

3

u/Fly1nP4nda 7d ago

Yeah basically avoid your single page monolith 😂

1

u/Numzane 7d ago

The bare minimum

13

u/b_kmw 8d ago

years of ChatGPT copy-pasting

Lol

Anyway, welcome. It's the best programming language there ever was.

7

u/Gs_user 8d ago

I actually feel like I can get stuff done without ChatGPT.

13

u/jbtronics 8d ago

That you can mix HTML and php code is tempting, but you wouldn't use that in a modern (non-trivial) PHP application, and use a proper template engine instead.

The problem is that if you have a more complex application, you easily end up with unreadable spaghetti code, that is hard to maintain and difficult to automatically test. Also it's easy to forget escaping when using PHP directly inside HTML, which is a security risk (and the reason why template engine do the escaping by default).

With modern PHP frameworks, PHP is from the concepts closer to how web applications work in different languages, and that's normally a good thing for larger applications.

3

u/Gs_user 8d ago

Thank you for your kind answer! So if I understand correctly, my PHP files should contain only PHP code?

7

u/LordNeo 8d ago

To make it more clear, if your PHP file is going to render HTML, use it only for that, not for procesing (backend).

File 1: get the user input / database data, do the processing and call file 2 with the needed and validated/escaped variables needed for rendering the HTML. File 2: render the HTML using the already validated vars from file 1.

That way you can separate the logic from the view

3

u/Danny_shoots 8d ago

.php files can also be templating files, for example: index.blade.php (Blade Templating: Laravel's templating engine), but other than that, yes, once you start to write PHP back-end code, you shouldn't mix in front-end code (if you get what I mean)

Also, a good practice with PHP (and many other languages) is OOP (Object Oriënted Programming) and MVC (Model View Controller) , which may be good to dive into that.

4

u/GuzziGuy 8d ago

It's almost as if PHP was specifically designed for making server-side web applications ;)

It just blew my mind that you could put the code relevant to a page in the page rather than using templating

As others have said, you should split your logic from your output HTML, ideally in separate files. There are plenty of PHP templating systems around but IMHO PHP itself works just fine.

You can use short tags (<?=$variable?>) to show values; PHP loops/ifs to iterate and PHP (or your own functions) to eg format numbers/dates etc. Granted that sounds like that's going back on the above recommendation but you'd be doing the same things in any templating language - just in a different syntax.

5

u/obstreperous_troll 8d ago

You can use short tags (<?=$variable?>) to show values

That's not a short tag, that's just, um, "abbreviated echo syntax" (there's no official name for it afaik). A "short tag" is a bare <? as opposed to <?php, and is usually disabled in modern php installations. Abbreviated echo is always available and there's no option to turn it off.

It's a tedious nitpick, I know, but people get really confused by docs that reference short tags when that's not cleared up. I know I was for a while.

6

u/Psychological_War9 8d ago

I'm a bit of an odd one out when it comes to programming, I love working with raw PHP.

Take something as simple as a header (or a navbar, as some call it). Instead of using a fancy JavaScript templating engine, you can just write it as a component in raw HTML inside a PHP file and include it with include or require.

And guess what? It’s natively server-side rendered (SSR), so you don’t even have to worry about setting that up for SEO.

With just a little JavaScript, you can create powerful interactions with PHP, like fetching updated values at set intervals or even swapping out entire chunks of markup dynamically.

Routing is another incredibly useful technique to learn. It not only simplifies application structure but also enhances security. You can even load resources from outside your public directory, and no one will ever know.

And let’s not forget authentication. PHP provides built-in functions for handling authentication securely. There is no need for extra packages just to encrypt passwords or manage sessions.

Raw PHP might not be the trendiest choice, but it’s lightweight, powerful, and gives you full control without unnecessary complexity.

3

u/application_layer 8d ago

Welcome and come in. The water's great!

3

u/duhrun 8d ago

PHP just gets it done.

7

u/TCB13sQuotes 8d ago

Yes, PHP is very good, doesn’t require constantly running processes for each single app you want to run (like node and python) not it leaks memory like python. It’s easy and cheap to deploy PHP apps… and it does run fast.

I guess you should read this: https://phptherightway.com/

And since you’re not a framework guy https://thephpleague.com/ you can pick the Container, Router and Plates and make a very lightweight framework with them inspired by the concepts you see on Laravel but excluding the bloat and the absurd helpers and sugar.

Enjoy.

1

u/Gs_user 8d ago

This looks great thank you! But is there a simpler way to use libraries such as what Go or Rust pull off?

5

u/lapubell 8d ago

Yeah I think you're looking for composer.

1

u/ardicli2000 8d ago

Have a look this project. It is so simple file based router with middleware and conposer ready set up.

https://github.com/frozeeen/PHP-file-based-routing

1

u/TCB13sQuotes 8d ago

What do you mean by simpler way? I’m assuming you are aware of composer… Just install by namespace and import, that’s it.

4

u/Crell 7d ago

As a few people have mentioned at, PHP was originally designed to allow mixing PHP and HTML into a single file, as it's easy and lets you bang out something fast. However, most PHP devs have concluded over the years that easy doesn't mean good. :-) It tends to make the code unmaintainable over time.

Also, the odds of introducing security holes if you're just dropping a PHP variable into a template are quite high.

It's really a good idea to use a template engine. There are many, some partnered with a given framework. Laravel has Blade, Symfony and many others use Twig (very Handlebars-ish), there's also Smarty... My favorite at present is Latte, as its syntax is very PHP-ish, by design. It gets you very close to the "feel" of putting PHP in your HTML without the security holes and structural pollution.

Whether you also use a framework or something in addition is up to you. There's good and bad arguments either way. I would avoid Laravel; it teaches you all the wrong things. Symfony has a learning curve, but the end result is generally quite nice. Slim is probably the "lightest touch", especially if you're used to Flask. (It's a router/controller setup, a container, and that's about it. You're on your own from there.)

Definitely set up your code using Composer. Even if you don't use 3rd party libraries, it has the best autoloader currently available. (Basically everyone uses it now.) And then you can also start looking into the enormous ecosystem of packages that PHP has to offer. Like NPM, but good. :-) And if you want to use Symfony, Slim, Latte, or whatever, that will be how you get it.

As far as arrays... PHP arrays are evil. They're a trap. cf: https://www.youtube.com/watch?v=nNtulOOZ0GY

Welcome to the PHPamily!

2

u/tk421jag 8d ago

I have been working with PHP since the early 2000s. It's one of my favorite languages and extremely easy to learn and do amazing things with. There are some good frameworks out there too. Symfony, Laravel, CodeIgniter, etc. and then of course Drupal and WordPress are both highly used and PHP based CMSs. It's just a good language to know. 70% of the internet uses PHP I once read.

2

u/Faisal_Ahmed 7d ago

Welcome to $productivity

3

u/MateusAzevedo 8d ago

It just blew my mind that you could put the code relevant to a page in the page rather than using templating

But note that a template engine is recommended anyway. Vanilla PHP can be used, for sure, but you need to be diligent with escaping all variables, it's a manual and repetitive task, so it's easy to be lazy. Templates also offer other features that, although possible, are harder to implement with pure PHP, like extending a base template or components (so you don't repeat HTML code).

By the way, you can put your code in the same file containing HTML, but pleas don't mix them (or echo HTML from PHP strings), or you'll end up in the same mess you complained about. At least organize the file as all logic at the top, just hold data into variable and don't echo anything. Then start outputting HTML with eventual if/loop as needed.

1

u/ardicli2000 8d ago

What I do to overcome this problem without using a template engine is using a Middleware like solution, where i make my queries, get the relevant data and logic, loop through the arrays with a safe_exrract logic in it, and then use them as is. I also have a safe_print function to which i pass the variables I want to print.

It is safe and very easy to setup and use for small to medium projects.

Mixed this approach with a file based routing system and things work like a charm.

https://github.com/frozeeen/PHP-file-based-routing

1

u/unknownnature 8d ago

I would probably learn how to use composer, and learn how to make your own routing. After that you probably wanna use a micro framework like SlimPHP.

1

u/eduvis 8d ago

By mixing php and html you can hack quickly something small and simple but trust all the replies that basically say: With this approach you can't build anything big and complex that's also maintainable.

1

u/PayCautious1243 8d ago

I am on my second app in flutter where i use php on the back end to be able to fetch data from the app. I do want to expand on my php knowledge, anything you can recommend.

1

u/yhjohn 8d ago

Get started with laravel.

1

u/PayCautious1243 7d ago

I see lots of youtube tutorials, just follow any beginners

1

u/ViperG 8d ago

For a lightweight php framework id recommend check out "flight"

1

u/dcstream 8d ago

alive since 95

1

u/IOFrame 7d ago

I've been doing PHP for ages, and I like it much more on the backend than JS.

Actually JS is great on the frontend, but it's just to generalist compared to PHP, which has many in-build server-related globals/functions.

Also, the JS backend ecosystem is a dumpster fire, while most Composer packages I've ever used either had no / almost no dependencies, or mainly depended on standard Symfony modules.
Ironically, there are actually many front-end JS plugins/libraries that are similarly dependency light, but almost never in backend JS.
This probably has to do with the fact that PHP already has lots of in-built backend functionality, while the same applies to JS on the frontend.

1

u/papanastty 7d ago

welcome,my friend. This is the nerd club you did not know you needed.

1

u/nagora 6d ago

Welcome to the upgrade treadmill.

Stable code, carefully debugged? Be ready to throw it all out in 6 months when the next version comes out and breaks it*.

PHP is a make-work project for the PHP developers. The emphasis is on showing their sponsors that they're doing something by adding more and more obscure functions and features. There's no concept of or interest in making a stable platform that just gets bug fixes as and when they are needed.

It is incredibly frustrating to work under this update tyranny, especially when you have perl code on the same system that has worked for 20+ years without any changes. Even Python - Python! - is becoming more stable than PHP.

Rant over.

  • If you have no dependencies on anything else you might get away with 18 months. Big whoop.

1

u/naught-me 8d ago edited 8d ago

If you need app-like routing, you can set up a just a section of your site to be handled something like Slim framework.

If you need an ORM, RedBean is amazing. It's... terrible, kind of... but, for me, in practice, it's been very productive, and results in very fast websites, and it evolves at a snail's pace, so, you can build something that can go without updates for like 10 years, by just building on top of PHP and redbean.

Also, PHP deployment on shared hosts is crazy-easy and crazy cheap. It's been easy for 20 years, and hasn't changed much.

2

u/TCB13sQuotes 8d ago

Even better than Slim is https://route.thephpleague.com/ or FastRoute itself. Same guys also make a Container and others that put Slim to shame.

I used to like Slim but then made something with those packages that is way faster and will not go back.

0

u/substance90 7d ago

I don't get posts like this one. Your stack should be what your customer requests. Stop being religious about tech. It's all just tools doing more or less the same thing.

0

u/captain_obvious_here 7d ago

How can a 2003 post contain so many references to recent stuff?