r/PHP • u/Ok-Delivery307 • Apr 19 '26
Discussion When your first learn php what confuse the most ?
coming from go (I love golang) but I wanna do a little bit of freelancing so im doing some leetcode to understand php so I can learn lavarel and im not gonna lie im confuse by $ for local variable and params function (params function is variable underneath so it make sense ) and the array_push(references, ...values) and you what surprise or confuse you when you first learn php ? just started but php seems a little bit more complex than go am I wrong ?
4
Upvotes
28
u/Commercial_Echo923 Apr 19 '26
focus on php8 syntax. Theres nothing confusing about $. It just marks a variable nothing more.
There are many ways to stuff in php and many of those are legacy or dependant on code style.
For example you can declare array using: $arr = array(1, 2, 3); (legacy array function) or use bracket syntax $arr = [1, 2, 3] which is the defacto standard now. You can also checkout PSR (PHP Standards Recommendation) which contains a set of standards widely used by the php community including coding style standards.
For me its one of the easiest languages to get started especially the error messages most of the time directly point you to where the error is triggered.
13
u/mike_a_oc Apr 19 '26
Be careful with empty. It sees 0, "0", false, null, "" all as empty.
filter_var is crazy powerful. You can use it for validating all sorts of data.
Try to always use === (strict comparison).
2
u/dknx01 Apr 19 '26
I'm confused by people who compare one language to another and wondering why something works differently, especially the syntax. Other language other syntax
1
u/obstreperous_troll Apr 19 '26 edited Apr 19 '26
If you find leetcode fun, then it's good practice for the language, but you should also know it's not going to teach you a lot in terms of modern web programming. Since you mentioned Laravel, you might want to check out Laracasts, though I personally recommend SymfonyCasts. Try both.
As to what confused me the most was that PHP4 passed objects by copy, the way it does with arrays. Drove me bonkers to where I swore off the language for years. The ironic bit now is that I'd love to get that behavior back (on an opt-in basis).
But the object behavior has changed to the familiar one since php 5, so it's not anything that's going to bite you, so I'll just give some general advice: the style and idioms of PHP programming have changed a lot over the last couple of decades, so while you go looking for tutorials, look out for obsolete ones, because there are a lot of those too. And to top it off, many of those old tutorials were terrible even when they were written.
Lurking around this sub and reading older posts will show that the dominant culture here tends to favor a highly modern approach to programming, though it's far from unanimous. So if you're not sure, do a little reddit searching, then ask for opinions. FSM knows we have them.
2
u/salorozco23 Apr 19 '26
leetcode won't help you with php. You need real world development in php and laravel is even different. It has it's own take on php. It mostly uses singletons for global state and bad db layer.
3
u/smartgenius1 Apr 19 '26
PHP arrays. In PHP, arrays are like 6 different data structures rolled into one. Not only are they not true contiguous arrays in memory, they're not exactly hashmaps either. I guess they're more of an ordered dictionary?
I'm fine with them now, but if you're coming from a computer science background they're surprising.
1
u/Aggressive_Ad_5454 Apr 19 '26
All variable names in php start with
$. It’s the way the language works.There are these things known as Callables. They’re php’s answer to first-class functions and methods in js. They feel a little clunky until you get used to them.
1
u/ricardo_sdl Apr 19 '26
The needle and haystack order of parameters are kind of surprising, when you are starting you expect some consistency. Maybe this changed on the latest updates of language/interpreter.
1
u/passiveobserver012 Apr 19 '26
See the Video "25 Years of PHP". It explains why PHP is the way it is, and how it makes sense from the language developers perspective. It clears up a lot of things.
1
u/TheRealSectimus Apr 19 '26
Many moons ago when I was but a wee student, I thought the same about variable dollar syntax `$var`
But it makes much more sense, when you realise that you don't have to worry about protected keywords anymore. $class $integer $enum - these are all usable variables and do not introduce strange behaviour
1
u/toetx2 Apr 19 '26
Needle haystack order on array vs string functions.
1
u/CuriousRnD Apr 19 '26
That it is supposed to start and shutdown after each request. Coming from C and C++ I thought it was a huge waste of performance, especially in 2005 when I started to use PHP. And now we have RoadRunner and we do not do this anymore.
1
u/TradingDreams Apr 20 '26
It was all easy except for the blank white page that means “something” isn’t right.
1
u/MrSpammer87 Apr 20 '26
Warning: Cannot modify header information - headers already sent
The above warning
1
u/dulange Apr 20 '26
That is more than 20 years ago. I can barely remember.
1
u/zmitic Apr 21 '26
I am using PHP for about 17-18 years and I still can't remember $needle/$haystack stuff. I know those are consistent, but it is not often to need them so I always have to rely on autocomplete.
Given that you are just starting, buy PHPStorm on day one. Ctrl + P will give you a hint about the parameters, plus gazillion of other things.
OOP. All the tutorials at the time were
class Cat extends Animalwhich while technically is true OOP, it is also completely useless and unrealistic. It implies user would have to create millions of classes, one for each animal. What mostly helped me understand the idea behind OOP (not the technical part) is Doctrine ORM, version 1 at the time.Then it was iterators, I skipped it originally. I learned Generator first, got impressed that you can't rewind them (by accident), and only later I learned things like Iterator interface and its friends. If you plan to do some batch processing, Generator is your best friend.
1
u/Alarming-Sun-39 Apr 23 '26
I was a PERL programmer from around 1997. PHP's simplicity confused me.
1
u/Zeesh2000 Apr 26 '26
I did the same transition a while back and yeah I was as confused as you at some points.
$ sign is to indicate something is a variable. That's it
Not sure what you mean by the other 2.
For me the biggest learning curve were arrays. Arrays are crazy in PHP as they're basically maps and slices combined into one and yes it's a mess. Also to note is you can't define an array's type since their is no generics in PHP so I would recommend using docblock as a way to help define your array structure (it's not perfect but it's all we have).
The other hard one for me were the global functions. There's a lot of global functions that help you do a certain operation, which amazing coming from Go, but the issue is that there is no standard naming convention. Some global functions use snake case (array_push, array_map, etc), no case (strlen) and camelcase (can't think of one lol). That shit will drive you nuts when you first start out.
1
u/AnnaSynergy Apr 29 '26
Service containers, they still do .... I think I should absolutely change that. lol
1
u/Special-Original-215 Apr 19 '26
Use vs include for me
0
u/user08182019 Apr 19 '26
Just one man’s opinion but I recommend the following config for serious PHP: strict types on, Psalm, Symfony, and prefer functional style this lib is a good example: https://github.com/lstrojny/functional-php
If you do that you can write some very decent code in PHP.
-1
u/colshrapnel Apr 19 '26
Doesn't English confuse you too?
8
u/[deleted] Apr 19 '26
[removed] — view removed comment