Learning PHP Day One

Today, I began my journey of learning PHP. In my first session, I learned the basics of configuring XAMPP for PHP and where to place files to run them. I also learned about declaring variables, variable types, and how to display output using the echo statement.

Configuring XAMPP for PHP

XAMPP is a popular software package that includes Apache, MySQL, and PHP. To configure XAMPP for PHP, I had to follow these steps:

  1. Download and install XAMPP on my computer.

  2. Start the Apache and MySQL services in XAMPP.

  3. Create a new folder in the htdocs directory of XAMPP to store my PHP files.

  4. Open my web browser and navigate to http://localhost/myfolder/myfile.php to run my PHP file.

Declaring Variables in PHP

In PHP, variables are declared using the dollar sign ($) followed by the variable name. Variable names must start with a letter or underscore, and can contain letters, numbers, and underscores. Here's an example of declaring a variable in PHP:

$name = "John";

Variable Types in PHP

PHP supports several variable types, including strings, integers, floats, booleans, arrays, and objects. To declare a variable with a specific type, I can use a type-casting operator or a settype() function. Here's an example of declaring a variable with a specific type:

$age = (int) 27; // integer
$salary = (float) 5000.50; // float
$isEmployed = (bool) true; // boolean
$friends = (array) ["Alice", "Bob", "Charlie"]; // array

Displaying Output in PHP

In PHP, the echo statement is used to display output to the browser. The echo statement can display text, variables, and HTML tags. Here's an example of using echo to display text and a variable:

$name = "John";
echo "Hello, " . $name; // outputs "Hello, John"

Why Not To Use print in PHP

While print is also a statement used to display output in PHP, it has some limitations compared to echo. print can only display one argument at a time, whereas echo can display multiple arguments separated by commas. echo is also faster and more memory-efficient than print. For these reasons, it is recommended to use echo instead of print in PHP.