Home » Programming » PHP

How to Execute PHP from the Command Line (Bash/Shell)

Execute PHP from the Command Line/Bash

This article will quickly run through the ways PHP can be used from the Bash shell/command line, with examples. PHP is usually used for generating content to be served on the web – but it can also be used from the command line. This is usually done for the purposes of testing or finding out information about the PHP environment – but PHP can also be used for writing command-line scripts (though, again, it’s not really done that frequently – probably because there are better … Read more

Home » Programming » PHP

How To Get Info About your PHP Environment Using phpinfo()

PHP phpinfo()

This article will explain what the phpinfo() function does and how to use it to get information about the PHP environment on your system. Many PHP packages and platforms (like WordPress, Laravel, and Symfony) have system requirements beyond what is included in the base PHP installation on most systems. These required modules include things like support for encryption, database support, and the ability to make HTTP requests from within PHP. To ensure that your PHP environment meets these conditions before trying to run the software, you … Read more

Categories PHP

Home » Programming » PHP

Merging/Combining Arrays in PHP with array_merge [Examples]

PHP array_merge

This article will explain how to merge arrays using the PHP array_merge function and provide some code examples. What is an Array? An array is a type of PHP variable that can hold other values, or references to other variables, in a list at a certain position. Arrays are really useful – allowing you to store a list of variables or values of any length in a single variable – ready to be iterated over, printed as the rows in a table, or otherwise processed. Arrays might … Read more

Categories PHP

Home » Programming » PHP

Using PHP cURL To Retrieve Data or Talk to an API

PHP cURL

The PHP cURL library adds support for requesting and submitting data over HTTP connections. This article will show you how to use it. There are a plethora of useful online services you can pull data from. From mapping to currency conversion, weather, flight schedules, language translations (in fact, just look at a big list of them here), these services, called APIs (Application Programming Interface), allow you to interact with massive databases of information over the HTTP protocol. PHP includes the tools to do this in its … Read more

Categories PHP

Home » Programming » PHP

Formatting Numbers with number_format() in PHP [Examples]

PHP Number Format

This article will explain how to use the PHP number_format() function, with some helpful examples. Different regions use different number formatting – Some countries, including the UK, USA, Australia, and New Zealand, use a comma (,) to separate thousands when writing large numbers and use a period (.) as a decimal separator. Your computer most likely uses the same formatting when storing number values in a database. However, some countries, like Germany, use a period as the thousands separator and a comma as the decimal separator. Others … Read more

Categories PHP

Home » Programming » PHP

Using PHP unset Function to Destroy a Variable, With Examples

PHP unset()

Here’s a short article on a useful PHP feature – the unset construct allows you to destroy a variable, making it unavailable for use. There are a few reasons why you might want to unset a variable: Making sure you don’t accidentally use a variable you don’t want to use in a certain context or scope Cleaning up variables after a loop Make a global variable unavailable within a function or scope Remove an element from an Array Remove an object attribute Read on to see how it’s done. … Read more

Categories PHP

Home » Programming » PHP

Encode Strings with PHP urlencode/rawurlencode [Examples]

PHP urlencode()

One way to pass data to a web page is via the URL query string. The data must be properly encoded – urlencode() and rawurlencode() in PHP do this. The PHP urlencode() function URL encodes strings in PHP and is widely used, but it is not the best tool for the job.  rawurlencode() is the modern replacement for urlencode() – though you may need to use the older urlencode() for compatibility if you’re working on older code. urlencode() Syntax urlencode($string) Note that: urlencode() will return a string variable containing … Read more

Categories PHP

Home » Programming » PHP

How to Use if…else Conditional Statements in PHP [Examples]

PHP If...Else

Here’s a handy guide on using if/else/elseif conditional statements in PHP to execute code based on certain conditions. if/else statements are found in pretty much all programming languages. Computer programs need to perform actions based on a user selection, or the result of a calculation, or the value received from a sensor – and if/else statements are how this is programmed into the system. When building even the most basic programs, you’ll use them, so they’re worth getting to know. PHP Syntax There are several parts to an if … Read more

Categories PHP

Home » Programming » PHP

How to Use the PHP echo Statement, With Examples

PHP echo

This quick tutorial will show you how to use the PHP echo statement to print/display information on screen. A computer program isn’t much good if it doesn’t display some output to the user. The echo statement prints a simple string of text in PHP – usually to the web browser. Syntax echo is a simple statement with a simple purpose – and it has simple syntax: echo expressions Note that: expressions should be one or more strings containing the text or characters you want to display on screen If more than … Read more

Categories PHP

Home » Programming » PHP

Print an Array in PHP using print_r and var_dump, With Examples

PHP Print Array

This article will show you several methods on how to print an array in PHP, with some examples. Arrays in PHP can be processed using foreach loops to interact with each element in the array. This article focuses on directly printing an array in full rather than each element individually. Using echo to Print an Array (It Won’t Work) You might think that you can just use the echo statement to print an array in PHP – this is not the case, as seen below: <?php // Define an … Read more

Categories PHP