How did you Learn to Program and Code?

my learning began with web development, which i started by looking the source code of existing websites.
i received a more overall study time, when i studied computer science for undergraduate.
i received a very practical learning experience when i began working as a software engineer.
after all of those experiences, i think the best way is to have an app in mind and just learn what you need in order to build it. but, if it's a copy and paste approach, it's not really going to work out, so you do need to understand your findings of how to do the basic building blocks of your goal product.

W3Schools is good place to start learning programming like, HTML, php, javascript, etc. but there is just basic knowledge you can get from there. you really need to start a project and practice more. otherwise, you cannot advance yourself.

For me, it started when I got my first computer.
The computer simply started with a command prompt, so no graphical interface (no windows, no mouse, etc). Incidentally this was also before modern OS existed, so this command prompt was not even "advanced" as MS-DOS.
So I started reading the instruction manual which stated with some "basic" commands, and slowly built from there, doing more "complex" things like a step calculator.
If you prefer "modern" code/programming language, then I started with a book (once again, yes, this was eons ago; before google existed).
If I was to start again now, then start with a more graphical/intuitive language. Learn the programming paradigm, then slowly move sideways to similar languages that share syntax.
In any case, having a good development/sandbox environment with debugging helps a lot to learn from one's mistakes.