From ef70990c08c8e3b9b8441c46c3ee2e7c2430b440 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 17 Nov 2023 20:36:48 +0100 Subject: [PATCH] added cheat sheet --- .../onepagers/docker-basics-onepager.md | 3 + en/content/onepagers/php-basics-onepager.md | 201 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 en/content/onepagers/php-basics-onepager.md diff --git a/en/content/onepagers/docker-basics-onepager.md b/en/content/onepagers/docker-basics-onepager.md index cabab08..f99a893 100644 --- a/en/content/onepagers/docker-basics-onepager.md +++ b/en/content/onepagers/docker-basics-onepager.md @@ -171,3 +171,6 @@ Unused resources accumulate with time, so it's good to run this command every on The [What are Containers?](https://edu.chainguard.dev/software-security/what-are-containers/) guide from Chainguard Academy has a nice high level overview of containers and images. For more technical specifications and reference docs, check the official [Docker Documentation](https://docs.docker.com/get-started/overview/) which covers all components in the Docker container ecosystem. For considerations about container security, check this Academy guide on [Selecting a Base Image](https://edu.chainguard.dev/software-security/selecting-a-base-image/) and the introduction to [Software Supply Chain Security](https://edu.chainguard.dev/software-security/what-is-software-supply-chain-security/), which should give you a better understanding of security considerations when bringing your images to Production. + +## Bonus: Docker Cheat Sheet +![Docker Cheat Sheet](https://cdn.erikaheidi.com/blog/docker-cheat-sheet.png) diff --git a/en/content/onepagers/php-basics-onepager.md b/en/content/onepagers/php-basics-onepager.md new file mode 100644 index 0000000..81c6e78 --- /dev/null +++ b/en/content/onepagers/php-basics-onepager.md @@ -0,0 +1,201 @@ +--- +title: PHP Basics for the Command Line +description: A light introduction to the PHP language and its general syntax focused on the command line +tags: coding, beginners +--- +PHP is an interpreted programming language that primarily runs on the command line, with additional software that allows it to serve content to web servers. A web server, such as Nginx, will make requests to a PHP service running either locally or on a remote location (such as, another server or container) in order to obtain the contents that should be made available to the user who originally requested the server's URL. + +With a modest learning curve and an extensive user-land ecosystem of community packages, PHP is an excellent choice for building command-line tools that can be used to query APIs, parse and generate content, talk with databases, and many other tasks that don't require a graphic interface. PHP CLI apps can run on schedule as cron jobs and as GitHub Actions. + +Even if your end goal is learning PHP to create web applications, limiting the scope initially to command line PHP will help you focus on the language syntax and specific constructions before having to deal with front-end and web requests. + +In this _crash_ course, you'll learn the basics about PHP programming for the command line. + +## Running PHP Code + +Before you're able to run any PHP code, you'll need to set up a PHP development environment. There are many ways to go about that, but for this mini course we'll use Docker, because it is multi-platform, and it allows you to run PHP code without having to install a PHP environment on your local machine. + +If you haven't yet, now is a good time to get Docker installed on your system. If you are on Linux, you have the choice to install either the [Docker Engine](https://docs.docker.com/engine/install/), which is a minimalist way to get Docker up and running on your system, or [Docker Desktop](https://docs.docker.com/desktop/), which offers a graphic interface to operate Docker. Windows and macOS users need to install [Docker Desktop](https://docs.docker.com/desktop/). + +Follow the installation instructions for your operating system. Then, confirm that you can run `docker` on the command line: + +```shell +docker --version +``` +You can now pull a PHP development image to execute your PHP code. I recommend [Chainguard Images](https://edu.chainguard.dev/chainguard/chainguard-images/reference/php) because they're minimal and always up-to-date with the latest patches for bugs and CVEs. + +To pull the image locally, run: + +```shell +docker pull cgr.dev/chainguard/php:latest-dev +``` + +```shell +docker run cgr.dev/chainguard/php:latest-dev --version +``` +``` +PHP 8.2.12 (cli) (built: Oct 24 2023 23:44:54) (NTS) +Copyright (c) The PHP Group +Zend Engine v4.2.12, Copyright (c) Zend Technologies +``` + +You should now be ready to execute PHP scripts on the command line. + +## Files and Execution + +PHP scripts are typically defined in `.php` files, although when working in the command line you may have PHP executables without an extension, just like with bash scripts. The PHP interpreter ignores anything that is outside a `` | more than | `6 > 5` //true | +| `<` | less than | `6 < 5` //false | +| `>=` | more than or equal to | `6 >= 6` //true | +| `<=` | less than or equal to | `6 <= 5` //false | + +### Logical Operators +These operators are typically used within conditionals to "short-circuit" the execution flow of a script. + +| | Operator | Examples | +|-----|------------------|---------| +| `!` | Logical invert | `!true` //false | +| `&&`, `AND` | logical **and** | `true && true` //true
`true && false` //false | +| `\|\|`, `OR` | logical **or** | `true && true` //true
`true && false` //true | + +It is easier to understand logical operators after you get familiar with conditionals, which we'll cover in the next section. + +## Conditionals +Conditionals allow you to test for values and change the flow of your code depending on the result of a logical expression. + +### `if` / `else` / `elseif` + +```php + 0) { + echo "condition is met"; +} else if ($test == 1) { + echo "" +} +``` + +### `match` + +### `switch` + +## Loops +Loops allow you to repeat a portion of code, typically until a certain condition is met - although in the command line you can have infinite loops that keep the application running until an interruption signal is received. + +In PHP, we have the following looping structures: `for`, `foreach`, `while`, and `do-while`. + +### `for` + +### `foreach` + +### `while` + +### `do-while` + +### Flow Control: `continue`, `break`, and `return` + +## Functions +Functions are blocks of code that can be reused across the application, accepting variable parameters, and may or may not return a value. + +```php +function greet(string $name): void +{ + echo "Hello $name!"; +} +``` + +It is possible to define default values for function parameters. When using this feature, any parameters that don't have a predefined value must come before the parameters with default values, for example: + +```php +function greet(string $name, string $greeting = "Hello"): void +{ + echo "$greeting $name!"; +} +```