php is a programming language. like most others, you can use it to write just about anything. but its specialty is the code that runs on servers to create websites. unlike most others, it was specifically designed for that purpose. it's been around for decades and, despite many predictions to the contrary, has remained the dominant language for backend programming on the internet.
a huge portion of the web runs on php. if you want to work on existing sites, that's a good enough reason already. popular software like wordpress, drupal and joomla are written in php. wikipedia, etsy, slack and even facebook were built on php. the most popular web interface for mysql -- phpmysql -- is also written in php, as you could guess from its name.
it's not just for legacy compatibility, though. php is the basic default language accepted by apache and nginx, the two dominant players in the web server market. they can certainly work with other languages but they run php right out of the box.
php is completely cross-platform, able to run on linux/bsd, mac and windows with the same code. connecting to something specific in those systems might require special changes but most code simply works no matter what you run it on.
realistically, though, there are two simple reasons to learn php today.
first, it has spent thirty years proving that it works on the web.
second, while it's not the easiest language to master, it is definitely one of the fastest to get up to speed on.
it also has more documentation and popular help available than just about any other language so, if you run into a problem, someone else has already solved it.
is this a reason to learn php and stop there, rejecting other languages? absolutely not. if you're developing for the web, you'll absolutely want to learn javascript. if you want to do app development, learning c, c++ and swift will serve you well. but, even if you have never written code before, php is a good place to start.
php is runtime-compiled. that makes it different from languages like c, c++, swift and many others that need to be compiled into a "binary" -- an executable file -- before being run. this has advantages and disadvantages. compiling a program into binary code means it runs much faster. that, of course, is the advantage. in addition, once it's been compiled, you can share the binary and it remains the same so people don't mess with it and introduce new errors. that is both advantage and disadvantage. with php and other runtime-compiled languages, anyone can edit it at any time, potentially causing problems but giving developers the ability to make changes as necessary without having to recompile the whole program. that makes development more granular and makes customization much easier. the disadvantage, however, is the other side of that coin. runtime-compiled languages are slower.
the main advantages to php are that it is supported across platforms and by just about every web server, that it is a language with a long history of community support and that it is an open standard -- no big company out there can pull the rug out from under your development or start charging you money. more on that point -- php is free, start to finish. you can write it in any text editor, host it on any server and integrate it with any other project or components in the language -- or even in other languages as long as they run on that server. you can run it from the command-line.
you don't need a local testing environment but you can certainly use your own machine if you want to. you can edit php files live on a testing server -- or even a production machine in an emergency -- with nothing more than the text editor on the command-line. whether that is an upside or a downside is all about perspective. it means there are no training-wheels like a dedicated development system. apple provides xcode for writing apps for ios and macos, for example. microsoft provides visual studio for developing for windows. these are huge systems that provide both a lot of assistance and a lot of overhead. there are helpers for php but, because it is compiled as needed instead of by the developer, it just remains a collection of plain-text files. that gives more flexibility as well as more opportunity to make errors.
php is a very forgiving language. there are often many ways to do the same thing and a lot of minor errors simply get overlooked and ignored. that is both good and bad. it can mean and error will pop up long after a file has been tested. but it means many things simply work the first time, something development in a lot of other languages tends not to result in for beginners.
there are several things to know about php before you begin your learning journey.
while you can run it on the command-line, it is designed to run on a web server and that's how we're going to be using it so you'll need to have one. whether you use your own computer or a separate one makes no difference to the language or your learning. just remember we will be talking about the "web server" and, if you've chosen to use your own computer as one, that will also mean your computer.
in many languages, whitespace is important -- things like spaces between symbols and characters as well as indentation. php ignores it. that might seem like an extremely technical place to start but there's a good reason to begin there. every example here will have whitespace and indentation that makes logical sense to make it easy to read and understand. but you will see many examples that use whitespace and indentation differently. it's important to know this makes no difference. here's an example...
function hello(){
$string = "hello world!";
print_r($string);
}
is no different in php from...
function hello(){$string="hello world!";print_r($string);}
the only thing whitespace does is makes it easier for humans like us to understand when we're writing and reading the code. it helps both us and anyone who needs to read and edit the code in the future. but, unlike many other languages, it doesn't change how the code runs. if you're familiar with a language like python, for example, it is famous for how picky it is about whitespace. php is exactly the opposite.
you can use any text editor to write php. php files are just text files. there's no need to think about them as being anything more complex than that. they're not like pdf or word documents, things that need to be opened in specific programs. "text editor" on a mac, "notepad" on windows or any of the built-in text editors on linux can open and edit any php files -- yes, this goes for every php file out there on the millions of servers running it in production settings. this isn't a special beginner thing where we're starting slowly. every php file is a text file.
but you shouldn't use the mac text editor or windows notepad to write php.
there are text editors that can check your php as you write it, connect to your web server automatically, color-code what you type to make it easier to understand, make everything look pretty with standardized whitespace and indentation and even give you direct access to the php language reference so you don't have to keep looking things up somewhere else. in fact, there are hundreds, if not thousands of text editors with those capabilities.
i recommend zed. it is free, open-source, cross-platform and easy to use.
there's an even more important reason to use a code editor like zed, though. you will start learning php by writing a single text file but, within a few hours, you will have moved on to multiple files in a single project. an editor like zed will allow you to open whole folders and see how the files interact with each other without having to have them open in different windows like you would with notepad or text editor. it will just make your life dramatically easier.
some people use command-line editors like nano or vim/neovim to edit php and there are definitely times when that will make your life easier for quick changes or in emergency situations. but, unless you're really looking to make your life difficult, i recommend avoiding them.
we often think of a web server as a machine. while that's certainly one answer, when we talk about web servers here, we mean the software, not the hardware. there are many options for web server software -- the two you will likely hear about most often are apache and nginx, both natively supporting php. but they all really do something very simple. they take information from the web -- like someone typing a web address or filling in a form -- and run a program. in the case of php, the program can do things behind the scenes but what the user sees is the text the program writes in return.
that text can be nothing more than basic letters or it can be html, javascript, css or anything else your web browser can understand.
we will get to it shortly but one of the ways to write things in php is to use the print_r command, which you've already seen in the whitespace example. if i have a php program and the only thing in it is print_r("hello");, the web browser will display hello. if it, instead, has print_r("<h1>hello</h1>");, the web browser will still display hello but it will be formatted like a title.
that brings us to an important point. the web runs on html. that's what browsers understand. no, you don't have to know html to learn php but you certainly do to create anything usable on the web.
html is very simple. it's just a markup language so it has no commands or variables or anything like that to learn. just codes to tell whether something is a title, paragraph, table cell, etc. you can absolutely learn php without knowing html but it's something you will absolutely want to know if you're interested in web development. i assume, given that php is almost exclusively used for that purpose, you are. i'm not going to teach you how to write html here but i will assume you have at least basic familiarity with it because it will show up in many of the examples.
the most popular web servers -- and the most popular for php -- are apache and nginx. i have used both for years and can recommend them both. to continue from here, you will need a web server, either on another computer or your local machine. if you don't have one installed, i suggest installing either apache or nginx because they're both simple to get started with and work with php without any extra configuration. how to install them depends on what system you're using but you can find instructions for apache at https://httpd.apache.org/ or nginx at https://nginx.org/. if you are using a web server somewhere other than your local machine, you can use whichever php server is already installed and it should work for everything you learn here. apache and nginx as well as php itself are completely free. you shouldn't face any cost barriers in learning php.
you're here to learn php. not to see if your computer already knows it. if you're going to use generative ai to help with your assignments, you can stop here and give up.
here's the deal. you have a choice whether to give up on using your brain and let computers do your thinking for you or to actually think for yourself.
i'll walk you through the learning process if you're here for it.
if you don't want to think, though, don't bother to pretend to go through the motions. you're not getting graded and i'm not giving participation trophies. if you get to the end of this and you don't know how to write php, you don't win anything. it's ok if you just want to give up and sleep your brain away.
but please don't bother to pretend. it's disrespectful to everyone around you who actually wants to think and learn.