r/PHPhelp • • Mar 21 '21

Best way to learn php?

Hey guys i have just started to learn backend. But I'm struggling real hard to get to know php/laravel. Please suggest me any beginner friendly book or yt channel or anything. Keep in mind i don't have any background in php/laravel or backend programming.

11 Upvotes

20 comments sorted by

18

u/olivermbs Mar 21 '21

Whilst a framework such as laravel is great for implementing best practices, it introduces a lot of “magic” which IMHO doesn’t help a beginner much with understanding core concepts. Dani Krossing on YouTube has a lot of good tutorials - start with the basics. When I moved onto my first framework after 2 years of just writing procedural spaghetti, many things started to click. Instead of immediately transitioning to a new shiny framework and dumping my old code, I started by refactoring my own code to be very similar to a framework (using auto loader, extracting all functionality into OOP classes, implementing a router, moving classes outside of my public root directory, separating view from logic, implementing PDO, normalising my database and implementing proper indices etc etc.) - the transition then became very logical. There’s also a video on YouTube where someone builds an MVC from scratch.. again very useful if you really want to understand how these things work.

Also: +1 for laracasts - this is a really structured approach to learning PHP and even some other languages.

12

u/ArlinnKordd Mar 21 '21

This is a fantastic answer +1

PHP For Beginners is a good Laracast to try along with the other tutorials mentioned in the comments.

Once you're ready to move on to OOP, I don't think the laracast for it is free, but I did a code-along including notes on GitHub that may be helpful.

Good luck and welcome, u/hafi51!

2

u/Between3and20charac_ Mar 21 '21

commented to follow this post. I am switching from DotNet to php becauseof the new job.

3

u/hafi51 Mar 21 '21

Good luck bro

4

u/Between3and20charac_ Mar 21 '21

thank you. Wishing you luck to be employed as soon as possible. Php is used a lot more in my country, so it was easier for me to get a php instead of dotnet job.

2

u/hafi51 Mar 21 '21

Same situation here. Btw where r u from?

4

u/CiiDyR Mar 21 '21

I can wholeheartedly recommend Laracasts. Should really get you going from Zero to Hero. Some Tracks/Lessons are free, but if you want to get the most out of it I'd recommend going for a subscription. Jeffrey is amazing and it's worth it all the way.

It's not just about Laravel, but teaches you pretty much all you need around it as well 👌

1

u/UnnamedPredacon Mar 21 '21

What's your previous experience?

1

u/hafi51 Mar 21 '21

None

5

u/UnnamedPredacon Mar 21 '21

I would recommend to start with PHP tutorials (some very good have been suggested), and then go to Laravel.

To make sense of Laravel you need a solid base. I vouch for the Laracasts too, excellent resources.

5

u/dietcheese Mar 21 '21

In that case, I suggest you ignore Laravel completely for now. Otherwise you'll end up copying code from Larcasts and StackExchange, and getting frustrated because you don't understand what you're doing. Just focus on PHP for a few months.

3

u/ahmadalmayahi Mar 21 '21

PHP: Start with “Programming PHP : Creating Dynamic Web Pages (4th edition)” written by “Kevin Tatroe” and “Peter Macintyre”.

Laravel: laracasts.com

3

u/N3crom0rph Mar 21 '21

As a beginner you want to learn some pure PHP first. Frameworks do a lot of stuff for you and that's great, but when something breaks, or you need to customize some behavior you will have no idea where to look if you don't know how it even works. So learn basics of the language, operations on MySQL, sessions/cookies, form handling among others. I used w3schools back in the day when I started but can't say how up-to-date it will be, it's been a few years. But there should be a lot of good and current sources on google and YouTube.

Then try to build a simple blog app. Nothing crazy, just user accounts, showing/editing/viewing posts and their comments and admin panel to manage it all. This will give you a good idea how a typical CRUD app works. After that you can start learning a framework, Laravel is popular, I started with Symfony and use it still. All of this is pretty much how I started ;)

3

u/IBurn36360 Mar 21 '21 edited Mar 21 '21

I'm going split this answer into 2 separate pieces, because this is asking 2 very different questions:

  • How do you learn how to program?

    • This is honestly the more difficult of the 2. Just like with spoken languages, once you know what to say, it is relatively simple to learn how to say it. In this case, programming in general could be considered the what to say to that analogy.

    • In my experience, people tend to learn how to program best, not by doing something as crazy as "Let's use a framework and make a simple website", as that kind of project has so much more than pure programming to it. You have to learn the framework, you have to plan out your project, you are working with at least 5 languages (Though not all of them programming languages). This kind of scope creep means that, while you will make something tangible, the actual experience you are looking to gather will be only a small part of the whole project and the quality of learning PHP will be relatively poor in comparison.

    • What I would recommend is make up and solve a small problem. In other cases, I have seen people make a simple representation of checkers, or make a math problem generator, etc. I would recommend making a pure PHP/HTML page that does the following:

      • Turn on error reporting on max. While you can make PHP do things and ignore all of the warnings, it is best to have warnings on when learning, as they will tell you exactly what you are doing probably wrong and help you write safer code. I would also NEVER use the silence operator (@), as it hides problems from you and I have yet to find a use case where I have needed it in several years now. There is always a right way to solve the error.

      • Every time the page loads, a simple addition problem is generated.

      • On every submission (Should probably be a POST request just to keep the URI tidy...don't need to deal with GET that just yet), the PHP will check to see what problem was submitted and check the answer.

      • If the answer was correct, display that to the user.

      • If the answer was wrong, display that to the user.

      • DO NOT use eval, not only because eval is a shortcut/problem maker and you want to learn, but because doing so will defeat the process of solving type casting. Try to actually work with casting the string you will get into numbers for the math, then cast the result back to a string for the display. While it isn't needed, not only will you know exactly what you are working with, but you will start being used to writing safe PHP.

  • How do I go about learning PHP?

    • To be honest, you need to simply learn the basics first. Learn what PHP gives you when it comes to built-in functionality (Especially strings and arrays. PHP handles these VERY well compared to every other language). Learn what a function is and its purpose, what a class is and its purpose, how do loops work, what an object is and its purpose. Don't dive deep into OOP, Factory Patterns, Singletons or anything like that because that doesn't matter right now. What you want is the most basic of basics, as many of the concepts in programming languages will translate. Once you get a good grasp on those basics, then you start really looking at the documentation and syntax. Anyone who has a good grasp on C-Like languages, of which PHP is one, can very quickly translate to any other C-Like language without any issue, only needing to look up functions and minor syntax differences.

    • One you have the basics mastered, learn them again, because your new experience will make you see all of these in a new light. Learn what constants are and how they should be used, what exceptions are and how you should create your own. Learn procedural programming, then Object Oriented Programming (OOP) and singletons, as they all have very important uses and knowing how each tool works is very important to being able to use them effectively. Then take a look at factory patterns and understand how they work on a surface level...specifically how you as a programmer would work with someone else's factory, as writing them is a whole other kind of learning process to begin with.

    • Now you can probably get into learning a framework. What one you chose doesn't matter, because the act of working with a framework is always the same. You learn the developer-facing API, try to use it, realize you didn't actually understand what they mean, repeat about 10 times, then actually have a grasp on what the framework wants from you. This pattern never really changes because it hits on a core problem of working with someone else's code...you didn't write it. As a result, you have to trust that the documentation is well written and extremely clear on what you need to do and, if not (probably not) do some trial and error and learn how the framework works (Or peek at the code, you should know enough by now that you can at least read it comfortably).

    • Once you get this far, it is time to start leaning the really crazy parts (Don't expect to get to some of these for years). Time to start dipping your toes into RegEx, as it really is one of the more powerful tools you can know, though the learning curve is so steep it tends to flat out go backwards most of the time and you will never fully remember everything about it, so keep a page or site bookmarked with helpers and a list of the tokens. Take a look into simple recursion. Don't worry about trying tail recursion yet, as you won't need that for likely a long while (Though it is useful). Look into how references work, specifically pass by reference and assign by reference. You won't use those often, but knowing how they work will open up another tool in the toolbox.

    • Once you get here, write your own template language, SQL implementation, heck even a TCP connection manager, because there is little else to learn, but so much more to do.

If you somehow got through all of that, then congrats and I hope that helped you out!

EDIT: I should have proofread. Fixes a few issues across the post.

2

u/emcoffey3 Mar 21 '21

Brad Traversy has a series of tutorial videos covering PHP on his YouTube channel. They're what I used to get up and running when I needed to learn PHP.

2

u/baohx2000 Mar 22 '21

To repeat what others are saying, DO NOT LEARN A FRAMEWORK. Learn the language.

If you feel you must, just start with a microframework where all you get is a router & DI.

1

u/[deleted] Mar 21 '21

start by building basic stuff, knowing what you have to do xyz for it to be secure etc and build on that

1

u/J3spah Mar 21 '21

I dont even know how i learned it