What is HomeBrew?
Homebrew is a powerful package manager for macOS that simplifies the installation of software and utilities. Whether you’re a developer, sysadmin, or a casual user looking to enhance your system, Homebrew offers a straightforward way to manage dependencies and applications. Here’s a quick guide to get you started with installing and using Homebrew on your Mac.
How to install homebrew?
- Head over to https://brew.sh/ and copy the following cmd
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Open the local terminal and run it.
- You then need to manually export the path for it to work
export PATH=/opt/homebrew/bin:/opt/homebrew/sbin:$PATH
- Once done, You can use it to install other packages
Installing Composer using brew
- open up the terminal and run the following cmd
brew install composer
- once promoted, press “y” and continue
- Now you need may need to add vendor files to $path
Setup IDE & Vscode
- Go through rtcamp guide for vscode setup. using the link
- Or you can quickly setup vs code using the following profile. link
- You can go to vscode, click on settings wheel and go to profiles
- Click on import and import using the provided link.
What is PHP?
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
- PHP is an acronym for “PHP: Hypertext Preprocessor”
- PHP is a widely-used, open source scripting language
- PHP scripts are executed on the server
- PHP is free to download and use
Why PHP?
- It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
- It is deep enough to run large social networks!
- It is also easy enough to be a beginner’s first server side language!
What is a PHP File?
- PHP files can contain text, HTML, CSS, JavaScript, and PHP code
- PHP code is executed on the server, and the result is returned to the browser as plain HTML
- PHP files have extension “
.php“
How it works? Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Example</title>
</head>
<body>
<h1>PHP Example Page</h1>
<?php
// PHP code block
$greeting = "Hello, PHP!";
echo "<p>$greeting</p>";
?>
<p>This is a static paragraph.</p>
<script>
// JavaScript code embedded in PHP file
console.log("This is JavaScript!");
</script>
</body>
</html>
Mid-Day Questions
1. Explain briefly about the Include vs Require vs Include once vs Require Once in PHP.
Include: It is mostly used to include another non-essential file such as header, footer or a notification card. If not found then it will continue executing rest of script.
Require: It is used to include essential components of site and In case any of required files are missing then it will throw an fatal error and stop execution of script
Ex:
index.php // requires that routes file is present
<?php
$document_root = $_SERVER['DOCUMENT_ROOT'];
require($document_root. "/../src/routes.php")
?>
routes.php // can include various files like this
<?php
include("home.php")
?>
Include_once: It is used to add a file a single time, if file is already present then it will not include it again.
Ex:
<?php
include("home.php");
include_once('home.php'); // will not be loaded again
?>
but if we switch order then it be loaded twice as when first statement ran, it was not present
<?php
include_once('home.php'); // loads home
include("home.php"); // again loads home
?>
Similarly, require_once: is used to load file a single time, and if not found it will throw fatal error and stop execution of script.
<?php
require("home.php");
require_once('home.php'); // will not be loaded again
?>
2. What is the difference between $GLOBALS, $_SERVER, $_REQUEST, $_GET and $_POST.
These variables are called superglobals in php.
Here is key definition for each :
$GLOBALS: are variables that can be accessed from any scope or functions. They can be used inside function by using global keyword or by using the $GLOBALS syntax.
Ex:
$data = “rtCamp”;
function findCamp() {
return global $data;
}
echo findCamp();
$_SERVER: are variables that hold information about server headers, paths, and script locations.
Ex:
echo $_SERVER['PHP_SELF'];
$_REQUEST: basically it is an array that contains data from other superglobals ($_GET, $_POST , $_COOKIES), It used to collect data.
$_POST: It is used to read the variables we received via html POST Request
Ex:
<form method=“POST” action=“login.php”
<input name=“name” type=“text” />
<button type=“submit” > Submit </button>
</form>
then in login.php
we can
<?php
$echo $_POST[‘name’];
?>
$_GET: It is used to access data such as Query string,
Ex: From URL.com/query=“rtCamp”
3. Where should we use the $_SERVER and $GLOBALS, and can we override them?
$_SERVER can be used to read the user agent and to see which browser a user is using
$_Global is used to store the variables that are used across scopes, usually its prefered to not use global variables.
Yes, both $_SERVER & $_Global variables can be override by us.
Ex:
$_Global[x] = "newValue" // sets new value
<?php
echo "Home";
echo $_SERVER["HTTP_USER_AGENT"]; // outputs "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
$_SERVER["HTTP_USER_AGENT"] = "NEW";
echo $_SERVER["HTTP_USER_AGENT"] //outputs "NEW"
?>
4. How to read XML files in PHP?
SimpleXML is a PHP extension that can be used to read data from xml file and manipulate them.
<?php
// $file = fopen('/Users/leaveitblank/Local Sites/leaveitblank/app/src/test.xml', 'r');
$xml= simplexml_load_file("/Users/leaveitblank/Local Sites/leaveitblank/app/src/test.xml") or die("Error:cannot find or create object");
echo $xml->novel[0]->title . "<br>";
echo $xml->novel[1]->title;
?>
5. Let’s suppose I have a template HTML (such as an email template) and I want to put its content inside a PHP file, is it possible? Which functions can be used to achieve the same?
yes, just like require , include works for php file, it also works for html files.
Ex.
create an html file my.html
<html>
<body>
<h1>Hello from html</h1>
</body>
</html>
then import it like this
<?php
include_once('home.php');
include("my.html");
?>
Thank you for reading through the post… More will be added.