General purpose scripting with PHP - Part 1: Introduction
Published: 2022-05-08 17:14:50

### About this series

When people think of PHP, what often comes to mind is a 
programming language that is used to create web applications.

While that is true, and frankly the most common (and intended) use case,
PHP is not only a language for the web. It is also an excellent tool
for general-purpose scripting.

This series assumes you have previous knowledge of PHP, and will not
make any attempts to teach the basics of the language. Instead, we will
take a hands-on approach of using PHP as a scripting language and learn
through examples how you can automate tasks as well as generate and work with data.


### Why use PHP for scripting? Why not use Bash or Python?

This is a great question to ask, and one that is often overlooked.

If you are like me, you have probably worked with setting up web servers,
and you might have found yourself automating tasks with bash scripts.

Bash can be great to use when working in Unix environments, especially
on servers as it's present in pretty much all web servers.
However, it quickly becomes a hassle to maintain. And if you work across
operating systems, you will probably find yourself having issues
with getting it to work consistently. Python can also be useful
but is not always as readily available.

But if you are managing a web server, chances are that your site
runs on PHP, which means that like Bash, it's already installed.

So why not use PHP then? It works cross-platform, and is probably
already installed wherever you are working, and it's easy to use.

### My use cases for PHP scripting

Here are some areas I've found myself using PHP, outside of web development.

#### Automating tasks locally and in CI/CD pipelines

Since I'm a developer, I like to automate things. As the saying goes:
> "why spend five minutes on a task that can be automated in five hours?" ~ by someone

Where I've found PHP scripting to excel is when running more
complex tasks in GitHub Actions (CI/CD Workflows), as such I will
dedicate a few chapters just those since I found that to be fun and useful.

Some cool things I've automated in GitHub Actions using PHP scripts are:

- Setting up standardised test environments
- Extracting data and calculating average test coverage data
- Injecting HTML templates with dynamic content to create static deployment previews

#### Generating and manipulating data

I love working with data when building web applications. However, I find the
content I get when using mocking libraries to be a bit lacklustre. I often
instead find myself generating more complex data sets using PHP and then
using those when mocking.

Here are some examples of data I've generated and manipulated using PHP:
- 10 000 dates with weighted timestamps
- Markdown files with front matter
- Realistic mock user data  

---

### Finishing up

This concludes this introductory chapter. I hope you enjoyed it and
are looking forward to reading the rest of the series
Please let me know if you have any questions or comments,
and please share the article and follow me for more!

You can also follow me on Twitter: https://twitter.com/StressedDev