What is ohmyzsh?

Oh My Zsh is an open source, community-driven framework for managing your Zsh configuration. Sounds boring. Let’s try again. Oh My Zsh will not make you a 10x developer…but you may feel like one!

How to install? you ask

You can install it using curl,

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

once installed, you are good to go.

What is phpcs? How to use it?

PHPCS is a command-line utility which can output varying levels of detail and evaluate one file, a whole directory, or a pattern match of target files. Its output is a list of flaws found, with an error message and line number supplied.

Use Case:
  • Define rules across projectsPHPCS can help enforce consistent rules across multiple projects in a team setting.
  • Share a custom rulesetPHPCS can help share a custom ruleset across an organization.
  • Identify unused variablesPHPCS can help detect unused variables, misplaced brackets, and overly long methods.
  • Set a maximum nesting levelPHPCS can help set a maximum nesting level and upperbound the Cyclomatic Complexity of methods. 

PHP: What are sessions & Cookies?

What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

To delete a cookie, use the setcookie() function with an expiration date in the past:

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>

What is a Session?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION.

<?php
// Start the session 
// nothing else should be before session_start()
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

To remove all global session variables and destroy the session, use session_unset() and session_destroy():

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

What are Filters? Whats their importantance?

PHP filters are used to validate and sanitize external input. The PHP filter extension has many of the functions needed for checking user input, and is designed to make data validation easier and quicker.

Validating data = Determine if the data is in proper form.

Sanitizing data = Remove any illegal character from the data.

The filter_list() function can be used to list what the PHP filter extension offers:

PHP: What are filters & Exceptions?

Why Use Filters?

Many web applications receive external input. External input/data can be:

  • User input from a form
  • Cookies
  • Web services data
  • Server variables
  • Database query results
PHP filter_var() Function

The filter_var() function both validate and sanitize data. The filter_var() function filters a single variable with a specified filter. It takes two pieces of data:

  • The variable you want to check
  • The type of check to use
<?php
$str = "<h1>Hello World!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>

Advance:

<?php
$str = "<h1>Hello WorldÆØÅ!</h1>";

$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $newstr;
?>

What is an Exception?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

Example:

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
} finally {
  code that always runs regardless of whether an exception was caught
}

What is OOPS?

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

PHP – What are Classes and Objects?

A class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Points to Note:

As oops concepts are same as java or any other programming language, there are few additional things to keep in mind;

  • PHP does not support method overloading in similiar way to java or any other languages.
  • PHP does not have multi inheritance using interfaces or class. We have traits for this use case.
  • There is no built-in method for type casting of user defined objects
  • Not supporting wildcard namespace import like Java and C#.
  • No nested/anonymous class support.

HTML CheatSheets

CSS cheat sheet- Interactive

https://htmlcheatsheet.com/css/

What is MySQL?

  • MySQL is a database system used on the web
  • MySQL is a database system that runs on a server
  • MySQL is ideal for both small and large applications
  • MySQL is very fast, reliable, and easy to use
  • MySQL uses standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use
  • MySQL is developed, distributed, and supported by Oracle Corporation

PHP + MySQL Database System

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

MYSQL: cheat sheet

https://github.com/methylDragon/coding-notes/blob/master/MySQL/01%20MySQL%20-%20Introduction.md

Mid-Day Questions

1. Which function will you use to check if a query parameter verify_code is a) not null? b) not an empty string?

We can check the two condition using following functions,

is_null() function checks whether a variable is NULL or not.
empty(variable) function checks if a variable is empty or not. We can combine it with is_string() to check if it’s a string or not. Alternatively, we can use strlen($string) to check the length of a string and if it’s greater than 0 then it’s not empty.

Ex: 
<?php
//  a part
 $check = null;
 echo is_null($check);

// b part- first approach
 $str = "";
 echo strlen($str);

//b part - second approach
 $stringData = "";
 echo  is_string(!empty($stringData)) ? "True":" false";
 ?>
2. What will you use, for calling the same function with different parameters within a class? Is there any way to have the same function within class but with different parameters?

There are two ways to achieve this behavior, first would be using … operator in front of the function parameter and second would be using __CALL function.

__CALL: When you call the get_value method, PHP checks for a method with the same name in the class, if it doesn’t find it, it calls the __call method instead, and that is how the code is run.

…$args: Simply allows us to have any number of arguments in the functions.

Ex:  

<?php
  class Test {

    public function __CALL($get_value,$data){
        $count = count($data);
        for( $i=0; $i< $count; $i++){
            echo $data[$i];
        }
    }
    public function get_values(...$args){
        $count = count($args);
        for( $i=0; $i< $count; $i++){
            echo $args[$i];
        }
    }

  }
  $obj = new Test();
  $obj->get_value(1, 2, 4);
  $obj->get_values(1, 23, 214);

 ?>
3. How to debug the errors in a PHP file? How do you tackle Fatal errors? Is there any way to avoid fatal errors?

There are multiple ways to debug errors in php, such as die(), setting up ini.php file to throw errors or using try catch blocks to throw custom errors by segmenting the code blocks in a file.
“`php Here is an approach with die():

Ex. Try Catch
1) {
throw new Exception("greater than 1");
}
return true;
}

//trigger exception in a "try" block
try {
checkException(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}

//catch exception
catch(Exception $e) {
echo 'Error:' .$e->getMessage();
}
?>
4. What is the difference between Global Variable and Class Variable?

Global Variables: are defined in a global scope outside any functions or class, they may be used inside a function using global keyword or by using $_GLOBAL[“va”] syntax.

Class Variables: are defined in the scope of class and can be used by other functions defined inside the class using $this keyword.

php
<?php
$varMine = "THIS IS GLOBAL KEYWORD";

function mycheck(){
global $varMine;
echo $varMine;
}
mycheck();

class Test {
private $value = 2;

public function get_value(){
    echo $this->value;
}
}
$obj = new Test();
$obj->get_value();

?>
5. If there are multiple tabs open in the browser, will all pages with their javascript run simultaneously or only the currently open tab’s javascript will run? Explain briefly why.

Modern web browsers use separate threads to execute the js in pages. So having multiple pages open would simply execute the js simultaneously in different threads as they are not getting blocked by each other. However, there are options to disable this behavior in modern browsers to be more efficient and you can limit how many worker threads are active.

We shall continue in next blog…

Thank you for reading…