Skip to content

sahilrajput03/learning-php

Repository files navigation

Readme

TODO: 12 July 2025

  • Recreate the package.json and package-lock.json file in this project and also add .nvmrc file.
  • Also, add packge-scripts file for better package management in this project of the scripts used.

About live php server setup

Note: The setup is only tested on firefox and chrome ~ Sahil.

Setting up:

Step1: Clone this repo and start the php server:

git clone https://github.com/sahilrajput03/learning-php
cd learning-php
npm i
npm start

Step2: Install the browser extension so browser can know if some files have changed in disk and then it will simply 💓︎ refresh the server page on every change. You can install the extension by navigating to appropriate links for your browser.

Chrome extension page

Firefox extension page.

Note 😬︎😬︎: You need to set the setting in the browser extension like that else you are on your own 🙏︎ -

Help image here...

More knowledge about the setup

With live-sever cli your server will run on origin: http://localhost:8080 and thus you can configure your browser live-server extension like shown below exactly(Note: We are serving our php server @ port 3000, see scripts in package.json for more details).

Note you must NOT (although we want it to run at port 8080 only, live-server by default server files @ port 8080 though) specify --port 8080 in live-server command, coz that seems to cause any problem and live reloading in browser isn't working that way.

In future problems for the setup ?

The github repo of browser extension tool is here.

How did I learn about this setup ?

Src: https://stackoverflow.com/a/64542693/10012446

About apache2 serve

To serve a particular path with apache2, read the guide @ linux-walkthrough.md file.

Tips

  • Formatter (Source - ChatGPT):
  • Formatting php with Prettier (Src)
    • Format with Prettier: Just install this package npm i -D @prettier/plugin-php to your project and you are good then.., (i.e., saving a file now also formats php files as well).
    • NOTE IF your formatter does not work in VsCode then please check this as it helped fix my issue of formatting in vscode today 12 July 2025. Github Issue
  • Run command: php -S localhost:8000 to run current folder at 8000 port.

Read about this awesomeness @ SO's answer: https://stackoverflow.com/a/21872484/10012446

  • Via apache2: Visit: localhost/1.php to run a php file for browser.

  • The file name must have extension .php otherwise the html content will render for sure but the php code won't execute at all.

Browse localhost/docs to view the default page of apache2 server.

Code (autogenerated by auto-docs)

File - 1.php

<?php
echo '<h1>Hello World 123</h1>';
echo 'Hello World 123';

// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
// src: https://www.w3schools.com/php/php_comments.asp
?>

File - 2.php

 <!DOCTYPE html>
 <html>

 <body>
     <h1>My first PHP page</h1>
     <?php echo 'Hello World!'; ?>
 </body>

 </html>

File - 3.php

<!DOCTYPE html>
<html>

<body>
    In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

    <br>
    Note: However, all variable names are case-sensitive.
    See next example for that^^.
    <br>

    <?php
    echo 'Hello World!<br>';
    echo 'Hello World!<br>';
    echo 'Hello World!<br>';
    ?>
</body>

</html>

File - 4.php

<!DOCTYPE html>
<html>

<body>
    Note: However, all variable names are case-sensitive.
    <br>
    <br>
    <?php
    $color = 'red';
    echo 'My car is ' . $color . '<br>';
    echo 'My house is ' . $COLOR . '<br>';
    echo 'My boat is ' . $coLOR . '<br>';
    ?>
</body>

</html>

File - 5.php

<!DOCTYPE html>
<html>

<body>
    <?php
    $txt = 'Hello world!';
    $x = 5;
    $y = 10.5;

    echo $txt;
    echo '<br>';
    echo $x;
    echo '<br>';
    echo $y;
    ?>

    <p>
        PHP Variables

        A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

        Rules for PHP variables:

        A variable starts with the $ sign, followed by the name of the variable
        A variable name must start with a letter or the underscore character
        A variable name cannot start with a number
        A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
        Variable names are case-sensitive ($age and $AGE are two different variables)
        <br />
        <a href="https://www.w3schools.com/php/php_variables.asp">source</a>
    </p>
</body>

</html>

File - 6.php

<?php
// Use below coee to refresh the page every two seconds
// $url1=$_SERVER['REQUEST_URI'];
// header("Refresh: 2; URL=$url1");

echo '<h1>Hello World 123</h1>';
echo 'I am Superman';
?>

File - 7.php

<?php
echo '<h1>Hello World 123</h1>';
echo 'I am Superman 1';
?>

File - 8.php

<?php
function writeMsg() {
	echo 'Hello world, to alex sir!';
}

writeMsg(); // call the function
?>

File - 9.php

<?php
function familyName($fname) {
	echo "$fname Yup.<br>";
}

familyName('Jani');
familyName('Hege');
familyName('Stale');
familyName('Kai Jim');
familyName('Borge');

function familyName2($fname, $year) {
	echo "$fname Refsnes. Born in $year <br>";
}

familyName2('Hege', '1975');
familyName2('Stale', '1978');
familyName2('Kai Jim', '1983');
?>

File - 10.php

<?php

// eg from: https://www.w3schools.com/php/php_functions.asp
// In the example above, notice that we did not have to tell PHP which data type the variable is.
// PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

function addNumbers(int $a, int $b) {
	return $a + $b;
}

echo addNumbers(5, '5 days');
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

File - 11.php

<?php declare(strict_types=1);
// In PHP 7, "type declarations" were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
// To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.

// The Example is from: https://www.w3schools.com/php/php_functions.asp

function addNumbers(int $a, int $b) {
	return $a + $b;
}

// echo addNumbers(5, '5 days'); // 🛑︎ This throws error..
echo addNumbers(1, 3); // 🥝︎ This doesn't throw error.
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

File - 12.php

<?php declare(strict_types=1);
// 🤹︎🤹︎🤹︎ Amazing we can still use loosely typed bindings with php even though we specified "strict declaration" for php, yikes!
function addNumbers($a, $b) {
	return $a + $b;
}

// echo addNumbers(5, '5 days'); // 🛑︎ This throws error..
echo addNumbers(1, 200); // 🥝︎ This doesn't throw error.
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •