Category: PHP

  • PHP Syntax, Variables, Database Insertion, and Login System Tutorial

    PHP Syntax, Variables, Database Insertion, and Login System Tutorial

    The source material offers a tutorial on PHP programming, starting with basic syntax, variables, and data types. It progresses to more advanced topics like superglobals, form handling, and database interaction using prepared statements to prevent SQL injection. The tutorial includes building a calculator and a search system as practical exercises. Arrays, constants, loops, sessions, and error handling are discussed, along with implementing an MVC pattern for better code organization. The material also teaches security practices like sanitizing data and regenerating session IDs. The focus is on creating a login and signup system with error handling and form persistence.

    PHP Study Guide

    Quiz

    1. What is a superglobal in PHP, and why are they useful?

    Superglobals are built-in variables that are always available in all scopes. They provide access to information from various sources like forms, cookies, sessions, and the server environment. They are useful because they allow developers to easily access and manipulate data throughout their PHP applications without explicitly passing them as arguments.

    2. Explain the purpose of the $_FILES superglobal.

    The $_FILES superglobal is used to retrieve information about files uploaded through an HTML form. It provides details like the file size, name, temporary location, and file type, enabling PHP to validate and handle uploaded files effectively. This allows developers to manage file uploads, check file sizes, and verify file extensions.

    3. Describe how cookies work and how the $_COOKIE superglobal is used.

    Cookies are small text files that a server embeds on a user’s computer to store information. The $_COOKIE superglobal is used in PHP to access the values stored in these cookies. Developers can retrieve information about user preferences or track user activity using cookies and the $_COOKIE superglobal.

    4. What are sessions, and how is the $_SESSION superglobal used to manage them?

    Sessions are a way to store information about a user on the server-side across multiple requests. The $_SESSION superglobal is used in PHP to store and retrieve session variables, allowing developers to maintain user-specific data and state during a browsing session. This is important for login and other user management features.

    5. What is the purpose of the concatenation operator (.) in PHP?

    The concatenation operator (.) is used to join two or more strings together in PHP. It combines variables and literal strings to create a single, unified string. Concatenation allows developers to dynamically build strings by combining different pieces of data.

    6. Explain the difference between = (assignment), == (comparison), and === (identical) operators in PHP.

    The = operator is used to assign a value to a variable. The == operator is used to compare two values for equality, without considering their data types. The === operator is used to compare two values for identity, meaning they must be equal in both value and data type.

    7. Describe what operator precedence is and how it affects the evaluation of expressions in PHP.

    Operator precedence determines the order in which operators are evaluated in a PHP expression. Operators with higher precedence are evaluated before those with lower precedence. Parentheses can be used to override the default precedence and control the order of evaluation.

    8. How do you create an if statement in PHP, and what is its purpose?

    An if statement in PHP is created using the if keyword, followed by a condition in parentheses. The code within the if statement’s curly brackets is executed only if the condition is true. if statements allow developers to execute different code blocks based on certain conditions.

    9. Explain the purpose of a for loop and how it is structured in PHP.

    A for loop in PHP is used to execute a block of code repeatedly for a specified number of times. It is structured with three parts inside the parentheses: initialization, condition, and increment/decrement. The initialization sets a starting value, the condition determines when the loop stops, and the increment/decrement changes the loop counter with each iteration.

    10. How do you start a session in PHP, and why is it important to do so?

    A session is started in PHP using the session_start() function. It’s important to start a session at the beginning of any PHP script where you intend to use session variables to store user-specific data and maintain state. By starting the session the server is able to connect all the activity of the client to a specific user ID.

    Essay Questions

    1. Discuss the importance of data validation and sanitization in PHP web applications. Explain how superglobals like $_POST and $_GET are used to retrieve user input, and describe common techniques to protect against security vulnerabilities such as SQL injection and cross-site scripting (XSS).
    2. Explain the different types of operators available in PHP, including arithmetic, assignment, comparison, logical, and string operators. Provide examples of how each type of operator is used in practical PHP code.
    3. Describe the different control structures available in PHP, such as if, else if, else, switch, for, while, and foreach. Explain how each control structure is used to control the flow of execution in a PHP script, and provide examples of real-world scenarios where each control structure would be useful.
    4. Explain the concepts of local, global, and static scope in PHP. Describe how variables are accessed within different scopes, and discuss the implications of using the global keyword.
    5. Describe the process of connecting to a database using PHP Data Objects (PDO). Explain how to prepare and execute SQL statements, bind parameters, and retrieve data from the database. Discuss the importance of using prepared statements to prevent SQL injection attacks.

    Glossary of Key Terms

    • Superglobal: Predefined variables in PHP that are always accessible, regardless of scope.
    • Cookie: A small text file that a web server stores on a user’s computer.
    • Session: A way to store information about a user on the server-side and remember it across multiple pages of a website.
    • Concatenation: The process of joining two or more strings together.
    • Operator Precedence: The order in which operators are evaluated in an expression.
    • Assignment Operator: Used to assign a value to a variable (e.g., =).
    • Comparison Operator: Used to compare two values (e.g., ==, !=, >, <).
    • Identical Operator: Used to compare two values to see if they are the same and are of the same type (e.g., ===, !==).
    • If Statement: A conditional statement that executes a block of code if a specified condition is true.
    • Else If Statement: A conditional statement that extends an if statement to execute a different block of code if the initial condition is false and a new condition is true.
    • Else Statement: A conditional statement that executes a block of code if the if condition is false.
    • For Loop: A control structure that executes a block of code a specific number of times.
    • While Loop: A control structure that repeatedly executes a block of code as long as a specified condition is true.
    • Incrementing: Increasing the value of a variable, often by one (e.g., ++).
    • Decrementing: Decreasing the value of a variable, often by one (e.g., –).
    • Control Structure: A statement that controls the flow of execution in a program.
    • Variable Scope: The region of a program where a variable can be accessed.
    • Local Scope: Variables declared within a function or block of code that are only accessible within that function or block.
    • Global Scope: Variables declared outside of any function or block, accessible throughout the script.
    • Static Scope: A static variable retains its value between function calls.
    • Constant: A value that cannot be changed during the execution of a script.
    • Database: A structured collection of data stored in a computer system.
    • SQL: Structured Query Language, a standard language for managing and manipulating databases.
    • Data Type: The type of data that can be stored in a variable or database column (e.g., integer, string, date).
    • Varchar: A variable-length string data type in databases.
    • Integer: A whole number without any fractional part.
    • Float: A number that contains a decimal point
    • Signed/Unsigned: Specifies whether an integer data type can store negative values (signed) or only positive values (unsigned).
    • Auto-Increment: A feature in databases that automatically assigns a unique, incrementing value to a column, typically used for primary keys.
    • Primary Key: A column in a database table that uniquely identifies each row.
    • Foreign Key: A column in a database table that refers to the primary key of another table, establishing a relationship between the two tables.
    • SQL Injection: A security vulnerability where malicious SQL code is inserted into a database query.
    • Hash: A one-way function that converts data into a fixed-size string of characters, used for security purposes.
    • Salt: A random string added to a password before hashing to increase security.
    • PDO (PHP Data Objects): A PHP extension that provides a consistent interface for accessing different databases.
    • DSN (Data Source Name): A string that contains the information required to connect to a database.
    • CRUD: An acronym referring to the basic database operations: Create, Read, Update, and Delete.
    • Model: A class or component responsible for interacting with the data storage, such as a database.
    • View: The part of an application that is responsible for presenting data to the user.
    • Controller: A class or component that handles user requests and interacts with the model and view to generate a response.

    PHP Fundamentals and Database Interaction Overview

    Okay, I have reviewed the provided text and created a briefing document summarizing the key themes and ideas.

    Briefing Document: PHP Fundamentals and Database Interaction

    Document Purpose: To provide a consolidated overview of PHP concepts covered in the provided source material, focusing on superglobals, operators, control structures, functions, data types for databases, SQL operations, sessions, hashing, and basic MVC principles with error handling.

    I. Superglobals:

    • Definition: Superglobals are built-in variables that are always available in all scopes.
    • Examples:$_FILES: Used to grab information about files uploaded through an HTML form. “Using this super Global here just kind of like allow for us to grab information about files that the User submitted using a HTML form” This is useful for file size checks and extension validation for security.
    • $_COOKIE: Used to store or retrieve information from cookies stored on the user’s computer.
    • $_SESSION: Used to store or retrieve information from session variables, which are stored on the server. Example: “I could for example create a dollar signore session and inside the bracket I’m going to make sure to include a name for this session variable here so I could call this one username”. Sessions are useful for storing user-specific data like usernames.
    • $_ENV: Contains environment variables, which are sensitive data not meant to be accessible to users or other environments. “Environment variables are essentially very sensitive data that you want to have inside the particular environment that the user is working in so you know data that should not be accessible to either the user or other environments”
    • $_SERVER and $_GET are to be discussed in later videos.

    II. Operators:

    • String Operators: Used to concatenate strings. The . operator is used for concatenation: variable C = variable a . ” ” . variable b;.
    • Arithmetic Operators: Basic mathematical operators like +, -, *, /, % (modulo), and ** (exponentiation).
    • Operator precedence can be controlled using parentheses. Example: (1 + 2) * 4.
    • Assignment Operators: Assign values to variables. = assigns a value. +=, -=, *=, /=, etc., are shorthand for performing an operation and assigning the result back to the variable. Example: variable a += 4 is equivalent to variable a = variable a + 4.
    • Comparison Operators: Used to compare values.
    • ==: Checks if two values are equal (without considering data type).
    • ===: Checks if two values are equal and of the same data type.
    • !=: Checks if two values are not equal.
    • !==: Checks if two values are not equal or not of the same data type.
    • <, >, <=, >=: Less than, greater than, less than or equal to, greater than or equal to.
    • Logical Operators: Used to combine multiple conditions.
    • && (AND): Both conditions must be true.
    • || (OR): At least one condition must be true.
    • Alternative symbols for AND and OR: and , or (less common).
    • Incrementing/Decrementing Operators: Increment (++) or decrement (–) a variable’s value. variable a++ increments after the current expression is evaluated, while ++variable a increments before.

    III. Control Structures:

    • Conditional Statements: Used to execute different blocks of code based on conditions.
    • if: Executes a block of code if a condition is true.
    • else if: Checks another condition if the previous if condition is false. “the else if statement basically is just a chain that we chain behind our if statement and says okay so if the first if condition is not met then jump down and check for the next one in the chain list”
    • else: Executes a block of code if none of the previous if or else if conditions are true.
    • Switch Statements: Provides an alternative to multiple if/else if statements, especially when comparing a single variable against multiple possible values.
    • Match (PHP 8): A newer feature similar to switch, providing a more concise syntax for value matching.
    • Loops: Used to repeat a block of code multiple times. “a loop is a way for us to spin out a blocker code multiple times by just writing one block of code”
    • for: Repeats a block of code a specific number of times. Requires initialization, condition, and increment/decrement expressions. “a for Loop is a very basic way for us to spit something out depending on numbers”
    • while: Repeats a block of code as long as a condition is true. “as long as this variable here is equal to true then I want to Loop something out”

    IV. Functions:

    • Definition: Reusable blocks of code. “A function is essentially just a blocker code that performs a specific task and then if you want to perform the same task that function that perform then you simply go and reuse the code”
    • Scope:Local Scope: Variables declared inside a function are only accessible within that function. “as soon as we declare a variable inside a function it is locally scoped within that function so we can only access it within my function”
    • Global Scope: Variables declared outside functions are accessible globally, but require special handling to be used inside functions (e.g., using the global keyword or passing as parameters).
    • Static Variables: Static variables inside functions retain their value between function calls.
    • Parameters and Arguments: Data can be passed into functions as parameters, allowing functions to operate on different data.
    • Return Values: Functions can return values, which can then be used elsewhere in the code.

    V. Constants:

    • Definition: Named values that cannot be changed after they are defined. Defined using the define() function. “When you create a constant that you use capitalized lettering because it shows other programmers that this is a constant so it’s just kind of a visual indicator for other programs to know that this is a constant”
    • Constants are in the global scope.
    • Good practice to define all constants at the top of the script.

    VI. Database Interaction (MySQL):

    • Data Types: Used to define the type of data that can be stored in a database column. “each column inside a table needs to be defined with a data type in order to tell our database what kind of data do we expect to be put inside this column here”
    • INT: Integer numbers. A common device width to define is 11. “a typical thing that we do when it comes to just making websites is we just Define 11 in here as a default”
    • BIGINT: Large integer numbers.
    • FLOAT: Floating-point (decimal) numbers.
    • DOUBLE: High-precision floating-point numbers.
    • VARCHAR(size): Variable-length strings. The size parameter specifies the maximum number of characters.
    • TEXT: Large text strings. ” using text for for example comments and blog post and that kind of thing is kind of what we need to use this data type for”
    • DATE: Dates (YYYY-MM-DD).
    • DATETIME: Dates and times (YYYY-MM-DD HH:MM:SS).
    • Signed and Unsigned: For integer types, SIGNED allows negative values, while UNSIGNED only allows positive values, effectively doubling the maximum positive value that can be stored.
    • SQL Operations:CREATE TABLE: Creates a new table.
    • INSERT INTO: Inserts data into a table.
    • SELECT: Retrieves data from a table.
    • UPDATE: Modifies existing data in a table.
    • DELETE FROM: Deletes data from a table.
    • Primary Key (ID): A unique identifier for each row in a table. “basically whenever we have a table we want to make sure that we can find data inside this table very easily which means that having a ID for all the different rows we have inside the table is going to make it a lot easier to find it”
    • AUTO_INCREMENT: Automatically assigns a unique, incrementing value to the ID column for each new row. “Auto increment which is a type of column that’s going to automatically increment or increase the number one so we can easily find things using ID so that’s just for example to make it easier to find different posts inside our website”
    • Important: Do not manually change primary key IDs after they have been assigned as this will break relationships with data in other tables.
    • Database Connection (PDO): PHP Data Objects (PDO) is a consistent way to access databases. “PHP data objects is a way for us to create a database object when we want to connect to a database so basically we turn the connection into a object that we can use inside our phsp code and just refer to that object whenever we want to connect to a database”
    • Data Source Name (DSN): A string that specifies the database driver, host, and database name.
    • Try/Catch Blocks: Use try/catch blocks to handle database connection errors.
    • Prepared Statements: “prepared statement is going to go in and and just pre compile the query we have so so therefore the code is going to execute a lot faster because we’re basically telling it hey in the future we we need you to run this query but we are just not going to insert the data inside just yet we’re going to do that later which means that it’s going to pre compile everything and have it all prepared and ready when we actually finally do pass in all the bits of data” Helps prevent SQL injection attacks.

    VII. Sessions:

    • Definition: A way to store information about a user across multiple pages. “Sessions are basically information that you store on a user that has to be remembered across multiple pages”
    • session_start(): Starts a session. Must be called at the very beginning of the script. “So right now we don’t have a session going on inside ex example so we could actually go in and copy paste this information paste it over here just to make sure we have a session started on both pages and then with this we’re now going to go back inside the website refresh it”
    • $_SESSION: A superglobal array used to store and retrieve session variables.
    • Session ID Cookie: Stored on the user’s browser to identify the session.

    VIII. Hashing:

    • Definition: One-way algorithm that converts data into a fixed-length string. Used to securely store passwords. “hashing is whenever we perform a oneway hashing algorithm on a plain piece of text for example a password and then we basically convert it into a fixed length string that you can’t really look at and tell what exactly is this supposed to be”
    • Hashing algorithms are designed to be irreversible and computationally expensive.
    • Salt: A random string added to the data before hashing to increase security. “Essentially a salt is a random string of text that is going to be included inside the data that you provide the hashing algorithm so it’s going to mix the salt together with the text that you provided it and then it’s going to Hash it afterward to create a more unique and even harder to crack hashing algorithm to make it even stronger”
    • Pepper: A secret string added to the data before hashing (similar to a salt, but not randomly generated). “And essentially we’re just going in and we’ll combine in these three pieces of data up here so we have the sensitive data that the user gave us inside whatever input we have inside the website uh we do also have a piece of data called a salt and then we also do have our pepper”

    IX. Basic MVC (Model-View-Controller) Principles & Error Handling

    • Structure: The material references separating code into different files (e.g., including a “hash_password.in.php” file in an “includes” folder). It also hints at the idea of a Controller component handling user input and calling on Models (presumably for database interaction).
    • Error Handling: The code demonstrates basic error handling, such as validating user input and using if statements to check for errors. It also demonstrates a basic error handling setup where an empty array is used to store error messages that can then be displayed to the user.

    Key Takeaways:

    • PHP provides a variety of superglobals for accessing different types of data.
    • Understanding operators is essential for manipulating data and creating complex logic.
    • Control structures are critical for controlling the flow of execution in PHP scripts.
    • Functions are essential for code reusability and organization.
    • Securely storing passwords using hashing with salts is a must.
    • Basic MVC ideas such as utilizing controller functions can help to break code up and make it more maintainable.
    • Proper database interaction requires understanding data types and SQL operations.

    This document provides a high-level summary. Deeper dives into each of these topics can be found in the original source material.

    PHP Superglobals, Operators, Control Structures, and Variable Scope

    Superglobals, Forms, and Data Handling

    • What are superglobals in PHP, and why are they important to understand?
    • Superglobals are built-in variables that are always available in all scopes of a PHP script. They provide access to information about the server, the environment, and the user. Understanding them is important because they are essential for handling data submitted through forms ($_POST, $_GET, $_FILES), managing sessions and cookies ($_SESSION, $_COOKIE), accessing server information ($_SERVER), and more. They act as the primary interface between the PHP script and the external world.
    • How can I prevent users from crashing my website by uploading very large files?
    • PHP provides the $_FILES superglobal, which contains information about uploaded files, including their size. You can use this information to validate the file size before saving the file to the server. By checking if the file size exceeds a certain limit, you can prevent excessively large files from being uploaded, thus protecting your website from crashes or performance issues.
    • What are cookies and sessions, and how are they managed in PHP?
    • Cookies are small files that a server embeds on a user’s computer to store information. PHP provides the $_COOKIE superglobal to access and manage cookies. Sessions are a server-side way to store information about a user across multiple requests. PHP uses the $_SESSION superglobal to access and modify session variables. You must call session_start() at the beginning of each script where you want to use sessions. Closing the browser typically ends the session, though this can be configured.
    • How can I use PHP to retrieve data submitted through an HTML form?
    • When a user submits an HTML form, the data is sent to the server using either the GET or POST method. In PHP, you can access this data using the $_GET and $_POST superglobals, respectively. For example, if a form has a field named “username,” you can access the submitted value using $_POST[‘username’]. Always sanitize and validate user input to prevent security vulnerabilities.
    • What are operators in PHP, and what are some common types?
    • Operators are symbols or keywords that perform operations on one or more values. Common types include:
    • String Operators: Used to concatenate strings (e.g., .).
    • Arithmetic Operators: Used for mathematical calculations (e.g., +, -, *, /, %, **).
    • Assignment Operators: Used to assign values to variables (e.g., =, +=, -=, *=, /=).
    • Comparison Operators: Used to compare values (e.g., ==, ===, !=, !==, <, >, <=, >=).
    • Logical Operators: Used to combine or modify boolean expressions (e.g., && (and), || (or), ! (not)).
    • Incrementing/Decrementing Operators: Used to increase or decrease a number (++, –).
    • What are control structures in PHP, and why are they important?
    • Control structures are language constructs that control the flow of execution in a PHP script. They allow you to make decisions, repeat code blocks, and execute different code paths based on conditions. They are fundamental for creating dynamic and interactive websites. Key examples are:
    • If/Else/Elseif statements: Conditional execution of code blocks.
    • Switch statement: Multi-way branching based on a value.
    • Loops: Repeated execution of code blocks (e.g., for, while, foreach).
    • What are the key differences between the local, global, and static variable scopes in PHP?
    • Local Scope: Variables declared within a function have local scope and are only accessible inside that function.
    • Global Scope: Variables declared outside functions have global scope and are accessible throughout the script, except inside functions, unless explicitly accessed using the global keyword or the $GLOBALS superglobal.
    • Static Scope: Static variables declared within a function retain their value between function calls. They are initialized only once and persist across multiple invocations of the function.
    • What are constants in PHP, and how do they differ from variables?
    • Constants are named values that cannot be changed after they are defined. They are defined using the define() function or the const keyword (in PHP 5.6+). Constants are always in the global scope and can be accessed from anywhere in the script, including inside functions. Unlike variables, constants do not require a dollar sign ($) prefix. It is convention to name constants using capitalized letters to distinguish them from variables. Constants are useful for storing values that should not be modified, such as configuration settings or mathematical constants (e.g., PI).

    PHP Syntax Fundamentals

    PHP syntax dictates how to write PHP code without generating errors. Here’s an overview of key aspects:

    • Opening and Closing Tags: PHP code must be enclosed within opening and closing tags, such as <?php and ?>. The PHP parser searches for these tags to identify PHP code within a page. You can embed PHP code directly within HTML using these tags.
    • Semicolons: Each PHP statement should end with a semicolon (;), which signals the end of the statement to the PHP parser. However, the closing PHP tag ?> automatically implies a semicolon, so the last statement before the closing tag doesn’t strictly need one. It is recommended to include semicolons after every statement for consistency and to prevent errors.
    • Pure PHP Files: When a file contains only PHP code, the closing tag can be omitted. This is the recommended practice to avoid potential issues caused by accidental whitespace or new lines after the closing tag.
    • Embedding PHP in HTML: There are two ways to embed PHP inside HTML:
    • You can directly embed PHP code within HTML tags, echoing HTML elements from within PHP code.
    • Alternatively, you can split up a condition using opening and closing PHP tags around the beginning and closing of the statement to allow HTML to be written as HTML.
    • Comments: It is good practice to use comments in PHP code to explain what the code does. Comments are not outputted in the browser.
    • Single-line comments can be created using two forward slashes (//).
    • Multi-line comments can be created by enclosing the comment between /* and */.

    PHP Variables: Declaration, Data Types, and Superglobals

    Variables in PHP are named memory locations that store data. They are essentially labeled boxes that hold data, making it easier to refer to and handle data within code. In PHP, a variable is declared by using a dollar sign ($) followed by the variable name.

    Here’s a detailed overview:

    • Declaring Variables: To declare a variable, a dollar sign ($) is used, followed by the chosen name for the variable. For example, $name declares a variable named “name”.
    • Variable Names:
    • Variable names must start with a letter or an underscore.
    • After the first character, variable names can contain letters, numbers, or underscores.
    • It’s customary to begin variable names with a non-capitalized letter, and subsequent words in the name should start with a capitalized letter (e.g., $fullName).
    • Assigning Data: Variables are assigned data using the assignment operator (=). For example, $name = “Danny Crossing”; assigns the string “Danny Crossing” to the variable $name.
    • Referring to Variables: To access the data stored in a variable, refer to the variable by its name, including the dollar sign. For example, echo $name; would output the value of the $name variable.
    • Variable assignment: It is possible to assign the value of one variable to another. For example, $test = $name; will assign the value of the variable $name to the variable $test.

    Data types that can be stored in variables:

    • Scalar Types: These variables contain only one value.
    • String: Represents a piece of text. For example, $string = “Daniel”;.
    • Integer (int): Represents a whole number. For example, $number = 123;. Note that integers should not be enclosed in double quotes; otherwise, they will be interpreted as strings.
    • Float: Represents a number with decimal points. For example, $float = 256.78;.
    • Boolean (bool): Represents a true or false value. The values can be true or false. In certain contexts, 1 is evaluated as true and 0 as false.
    • Array Type: Arrays store multiple pieces of data within a single variable. For example, $names = array(“Daniel”, “Bella”, “Feta”);.
    • Arrays can be created using the array() construct or with square brackets []. The square bracket syntax requires PHP version 5.4 or higher.
    • Object Type: An object is a data type.
    • Superglobals: Superglobals are built-in variables that are always accessible, regardless of scope. Superglobals are accessed using a dollar sign $ followed by an underscore _ and a capitalized word. Examples include:
    • $_SERVER: Contains information about the server environment.
    • $_GET: Used to collect data from the URL.
    • $_POST: Used to submit data.
    • $_REQUEST: Used to collect data after form submission.
    • $_FILES: Used to retrieve data about files uploaded to the server.
    • $_COOKIE: Used to retrieve values from cookies.
    • $_SESSION: Used to manage user session data.
    • $_ENV: Used to access environment variables.

    PHP Superglobals: Understanding and Usage

    Superglobals are built-in variables in PHP that are always accessible regardless of the scope. Unlike regular variables, which may have limited accessibility based on their scope (e.g., local variables within a function), superglobals can be accessed from anywhere in the code.

    Key aspects of superglobals include:

    • They are predefined variables within the PHP language.
    • They are automatically available without needing to be explicitly declared or defined.
    • They can be accessed from any part of a script, including within functions and classes.
    • Superglobals are referenced by creating the dollar sign $ but then creating a underscore _ followed by a capitalized word.

    Here’s a list of common superglobals in PHP:

    • $_SERVER: This superglobal holds information about the server environment, such as server names, document roots, and request methods. For instance, $_SERVER[‘DOCUMENT_ROOT’] can provide the root directory of the current script.
    • $_GET: Used for collecting data submitted via the URL. Data is appended to the URL as name-value pairs.
    • $_POST: Used for collecting data submitted through HTML forms using the POST method. This method is commonly used for submitting sensitive or large amounts of data.
    • $_FILES: Used for accessing data related to files uploaded to the server. It allows you to retrieve information such as file size, name, and extension.
    • $_COOKIE: Used for retrieving values stored in cookies. Cookies are small files that the server embeds on the user’s computer to store information about their activities.
    • $_SESSION: Used for managing user session data. Sessions allow you to store information about a user that persists across multiple pages of a website.
    • $_ENV: Used to access environment variables. Environment variables are often used to store sensitive data that should not be directly accessible to users.
    • $_REQUEST: Used to collect data after HTML form submission.

    HTML Forms: Data Collection, Submission, and Security

    Here’s information about HTML forms, based on the provided sources:

    • HTML forms are used to collect data from users.
    • Forms can include various input elements like text fields, drop-downs, and buttons.
    • Key attributes of the <form> tag:
    • action: Specifies where the form data should be sent for processing. This is typically a URL or a PHP file.
    • method: Defines the HTTP method used to submit the form data. Common methods are GET and POST.
    • Form Input Attributes:
    • name: A reference name is needed so the data can be grabbed once the data is sent to the next page. When grabbing data using the name attribute, you grab whatever the user input.
    • value: With drop-down menus, the data selected when referencing the name will be the data inside the value attribute.
    • Labels: Use labels to improve form accessibility, especially for users with disabilities.
    • Submitting Data:
    • GET Method: Submits data via the URL, making it visible in the address bar. It is typically used to get data from a database.
    • POST Method: Submits data in the body of the HTTP request, so it is not visible in the URL. It is typically used to submit data to a website or database. It’s also useful for submitting more sensitive data.
    • Superglobals: $_POST is a PHP superglobal used to collect data from forms submitted with the POST method.
    • HTML Special Characters: When having anything shown inside the browser using PHP, escaping it using HTML special characters is important to prevent users from injecting code inside the browser. There are a couple of different ways to sanitize data, depending on the purpose.
    • HTML Entities: HTML entities can be used to sanitize data.
    • Form Validation:
    • Required Attribute: Though it can be added to form inputs, it is not secure to rely on HTML, CSS, or JavaScript for security.
    • PHP for Security: Server-side security (using PHP) is essential for validating and sanitizing user inputs to prevent malicious attacks.
    • Error Handling: When creating any sort of error handlers inside a script, it can be checked if any of the inputs have been left empty when the user submitted the form. You don’t want the user to be able to submit the form if there is no data to submit. It needs to require that they submit all the data.
    • Action Attribute: When submitting a form and sending the data to the same page that the user is on, one way to do it is to open PHP tags inside the action, then include the server super global and target PHP self. It is important to note that this is prone to hacking.
    • File Uploads: The $_FILES superglobal is used when a HTML form allows users to submit files.

    Databases and Queries: A Quick Reference

    Here’s an overview of databases and queries, based on the provided sources:

    • Databases:
    • A database is used to save user information, and is used whenever a website has to remember something.
    • A database is made up of tables where similar information is gathered. Tables give the data structure.
    • Each piece of data corresponds to a column. Each entry (or data) corresponds to a row.
    • Relational Database Management System (RDBMS):
    • There are many different types of database systems, called RDBMS.
    • MySQL is a commonly used database system, especially with PHP. MySQL servers are not the same as MySQL PHP functions.
    • Setting up a Database
    • XAMPP includes both Apache and MySQL servers. The Apache server is the web server where PHP runs. The MySQL server is the actual database server.
    • To manage a database, use a dashboard like PHPMyAdmin.
    • To create a database, select ‘databases’ and type in a name.
    • SQL:
    • SQL (Structured Query Language) is used to manipulate databases. With SQL, you can create tables, insert data, select data, or delete data.
    • To use SQL, make sure the correct database is selected. Then, click the SQL tab.
    • There are typical SQL lines of code that are used repeatedly.
    • It is useful to practice SQL directly inside a database.
    • Data Types:
    • When inserting data, each column inside a table must be defined with a data type. This tells the database what kind of data to expect.
    • Common data types include INT for integers, VARCHAR for character strings, DATE, and DATETIME.
    • INT stores numbers. BIGINT stores larger numbers.
    • VARCHAR stores strings. You need to define a parameter for the length of the string.
    • TEXT stores long text.
    • DATE stores year, month, and day. DATETIME stores date and time.
    • Queries:
    • A query is a request for data from a database.
    • CREATE TABLE: Used to create a new table in the database.
    • INSERT INTO: Used to insert data into a table.
    • SELECT: Used to select data from a table.
    • UPDATE: Used to modify existing data in a table.
    • DELETE FROM: Used to delete data from a table.
    • Prepared Statements:
    • Prepared statements prevent users from writing SQL code directly inside an input.
    • The query (SQL code) is sent to the database first. Then, data submitted by the user is bound and sent to the database afterward. Because the query is separate from the data, SQL code will not impact the query.
    • Prepared statements can use either named parameters or non-named parameters.
    • With non-named parameters, user data is replaced with question marks.
    • With named parameters, each piece of user data is replaced with a name. With name parameters, the order does not matter.
    • Joins:
    • A join is used to select data from two or more tables at the same time.
    • Types of joins include INNER JOIN, LEFT JOIN, and RIGHT JOIN.
    • With INNER JOIN, data is selected from two tables that have matching data.
    • With LEFT JOIN, the table on the left is the primary table. Even if a user does not have a comment, all the users will still be shown.
    • With RIGHT JOIN, the comments on the right side are the focus.
    🔥 PHP Full Course 2025 – Learn PHP from Scratch! 🚀
    PHP Mastery Course: From Basics to Advanced with Practical Projects & Exercises in One Video.

    The Original Text

    so welcome to a new version of my PHP course now in this video we’re going to talk a bit about what exactly PHP is and what you’re going to learn in this course here and why it’s going to be a little bit better structured than the previous one I have on the channel so let’s go and talk a bit about what exactly this course is and who it is made for now here at the beginning it is going to be a beginner friendly course so to speak my main goal is to make sure that people has never done PHP before and might find a little bit intimidating because they’ve never done a programming language before will be able to get into this course here and don’t find it overwhelming so that is going to be my main priority as we going on with these lessons here it is quite normal to find PHP intimidating if this is your first programming language so don’t be scared that this is going to be overwhelming cuz I will try to make it as understandable as possible for people who has never done any sort of programming before with that said of course this course is going to get more and more complicated and more and more advanced as we go on but here in the beginning it is going to be very beginner friendly so with that said let’s go and talk a bit about what exactly PHP is and what you can use it for now PHP stands for hypertex preprocessor actually it stands for PHP hypotext pre-processor it is what you call a recursive acronym when you have the word itself inside its own spelling now PHP is a language that are used mainly for making websites but it can be used for other things as well like for example creating a desktop application if you know how to do it but it is something that is more commonly used for web developments one of the reasons it’s so easy to use for web development is because you can very easily embed it into the HTML when you start creating a website using HTML and CSS and it is also a very easy language to learn compared to many other programming languages out there and one of the things about PHP you may not know is that is actually considered a serers side language meaning that the phsp you’re going to program is going to run on the server of your website but not actually inside the client which is inside the browser so languages such as HTML CSS and JavaScript which actually runs inside the browser these languages run differently than PHP which is actually running on the server instead this means that when you’re writing PHP inside your website you can’t actually see the code inside the browser which you can when it comes to for example HTML CSS and JavaScript so PHP is completely hidden since it runs in the server instead so with all that said let’s go and talk about the elephant in the room is PHP dead because when it comes to websites on the internet right now currently in 2023 we have more than 78% of websites out there that we know of that are using PHP as their backend language this means that PHP is currently massively dominating when it comes to the backend languages that we use for websites out there today but one of the reasons I do often hear that PHP is a dead language and you shouldn’t use it anymore has a lot to do with the fact that PHP is only used mainly for web development whereas other languages such as python is used for all sorts of things including you can also use Python for web development so if you were to take the Python programming language and say okay how many people are using python nowadays versus people who are using phsp then python is going to have much higher numbers however these are actually not the numbers you should look at since we need to look at how many people are using python 4 when it comes to web development versus people who use PHP for web development and when it comes to this PHP is much much much higher numbers than when it comes to python it pretty much BS down to the fact that some people on the internet don’t like PHP because it’s more specifically suited towards web development whereas a language like python can be used for many other things besides web developments so if you’re sitting there you want to learn specifically web development then PHP is by far the language that I would recommend using when it comes to web development of course python is also an amazing language to use for web development if you want to use D Jango as a framework but PHP is just more the more popularly used language when it comes to specifically web development so just to mention a couple of websites that do actually use PHP we do have Facebook which uses a version of phsp we do also have Wikipedia canvas is also a very popular website that uses PHP and then we do also have WordPress which is not really a website but more of a Content management system which is also the most popular content management system out there today so if you plan on using WordPress at some point in the future I do recommend that you learn PHP since it is what they use when it comes to plugins and just the the WordPress CMS system in itself learning PHP is definitely something that I highly recommend if you’re just planning on going into web development as a web developer but now let’s go and talk a bit about Theory versus practice when it comes to implementing PHP inside your website because when it comes to learning any sort of programming language like for example PHP then things will be a little bit slow in the beginning it is just a very typical thing when you learn a new programming language that you have to learn all the theory first and then later on you start getting into some more practical examples so you can actually see oh okay so that’s how we use it inside for example our website I will try to include as many examples as I can as we go throughout this series but it is important to know that there will be a lot of theory here in the beginning so you don’t need to worry too much about it if you’re looking at these lessons at the beginning and thinking to yourself okay so I can’t really see how this code that we’re learning has to be used inside a real website just keep following the lessons at at some point you will get to a point where you get a realization of oh so this is how we need to use everything everything that we learned inside a real website but now let’s go and talk a bit about how I am going to approach this course here it is my experience that people become very easily overwhelmed when it comes to teaching a backend programming language like PHP so it’s important to split things up into multiple lessons to make sure that people can digest it a lot easier that being said when it comes to teaching PHP you can roughly divide PHP into three different categories when it comes to learning PHP as a language you have the actual PHP language which is just learning PHP and how to write it and how to Output things inside your website you know just plain PHP programming and how to actually write things that do something inside your website then after learning PHP you’re going to start learning about databases and how to actually manipulate databases by pulling out data or inserting data inside the database a database is a place where we store information for example if you want your website to remember things about your users and then the last thing you need to learn about is security Now security is a huge when it comes to PHP since you are essentially manipulating data from the user and a lot of that data is going to be sensitive data so it is important to take security very serious when it comes to PHP because it is something that is crucial to learning PHP and you can’t learn PHP and just go into it with the mindset of okay so security is just kind of like like an offside thing security is something you need to do and it is something you need to look into at some point and it is something that we will start looking into a little bit further into the course later near the end I do want to point out here that I do know a lot of people think it’s very important that you teach all security at the beginning when a person starts learning PHP but in my experience a lot of people will get overwhelmed if you teach everything when it comes to security at the same time as you also try to teach a complete beginner the basics of PHP so in order to digest things a lot easier we’re just going to focus on phsp then later on we’re going to start learning how you need to implement security into the PHP you already learned of course there is going to be moments where we can’t avoid talking about security and when those moments come we will of course talk about some security but any security that isn’t directly related to any sort of lesson that we’re learning about is not something we’re going to talk about until later on so with that said let’s talk about some frequently asked questions since I do want to answer some of the questions I have received in the past in my comment section will I include documentation for each lesson yes there will be documentation for each of the lessons that I teach inside the description of the video so if you want to Deep dive a little bit further into the lesson that we’re learning about then you can of course look into that documentation and learn a little bit further about what we’re learning will we do procedural or objectoriented PHP programming now here at the beginning we will focus on doing procedural PHP programming since I do know that it’s easier to get people into PHP when it comes to procedural programming later on in the course we will of course Deep dive a little bit further into objectoriented PHP programming but when it comes to just PHP here at the beginning like I said it’s going to be procedural and just to mention it for any beginners watching this you don’t need to look up object oriented PHP just focus on procedural PHP and learning that and then later on we will get to do objectoriented PHP and talk a bit about what exactly it is will I cover a framework like larell now something people may not know about especially if you are a beginner when it comes to learning any sort of language like HTML CSS JavaScript PHP python whatever you’re trying to learn there will always B Frameworks now Frameworks is a way for us to follow a well framework in order to build things much easier much faster and just kind of like to automate some things for us and a lot of things when it comes to especially PHP and Security will actually be automated when it comes to doing something using larell for building PHP applications however since this is going to be a phsp beginner course and I do think it’s important that people shouldn’t even look at a framework until they learned the basics of PHP we will not cover any sort of Frameworks in this course here at most it is going to be a separate course at some point in the future but for now it is not going to be part of this course here so with that said I hope you enjoyed this little introduction here in the next episode we’ll talk about how to set up PHP and how to install a local server on your computer since we did talk about PHP being a serers side language so we do need to have a server in order to actually write PHP inside a website and again just to mention this for the beginners here you don’t need to freak out when I say you need to install a local server on computer it is something that takes literally a minute to do and it’s not something that’s going to break your computer anything it is something that everyone that does web development at some point will have to do when they start learning how to make websites so with that said I hope you enjoyed this little video and I’ll see you guys in the next [Music] [Music] one so in order to set up a website using PHP we have to install what is called a local server and there’s a lot of different software out there in the internet that you can get in order to install a local server on your computer I do know that some people are a little bit scared when it comes to installing a server on your computer and I just want to point out that there’s nothing to be scared of everything is going to be fine and you’re not going to install any sort of viruses anything setting up a server is something that is actually quite easy to do and anyone that does websites do it quite frequently so it’s not something that new people should be scared of doing it’s something that takes a couple of minutes to do and then you have something running on your computer so when it comes to installing a server there’s many different servers you can choose from you have lamp Vamp examp limp there’s many different kinds of servers I did also hear about something called Ducker from one of my subscribers in the last video so it’s just interesting to see that there’s so many different ways to do it what we’re going to use however is a server called exam and the argument I have for using exam is that it’s easy to set up and it’s the one I’ve been using for many years I’m just really comfortable using xam so going inside your computer you can see that we have this website here that I just found called Apache friends.org I’ll go ahe and leave a link to it so you can actually see it on screen here basically this is just going to be a piece of software that you’re going to download that we’re going to start and then it’s going to run our server on our computer this means that we can actually run a website that is using PHP on our computer without having to upload our website to the Internet so this makes it very easy to just work on our website offline on our computer just like if you were to just make make a HTML website as you can see we have a couple of different versions We can install in here we have for Windows Linux and mac and you can also see what version we’re going to install in this case here this is going to be release 8.2.0 which is the PHP version that we’re going to run on This Server here so once you figured out what operating system you’re sitting on I I bet you probably know already you’re going to go and click the button for that one so I’m going to click windows then it’s going to install the program for you and if it doesn’t you’re just going to go and click up here where it says click here then we’re going to accept the privacy up and then we’re just going to go and download the latest version which is 8.2.0 so we have the latest version of PHP here so I’m going to go and download it now once you have it downloaded you’re just going to go and double click it so we can make sure to install it on our computer and it is important that you take note of where exactly you are installing it since we will have to go in and do a couple of changes to it now if you do get a popup like this don’t worry too much about it since this is only going to be relevant if we were to install this inside our program files inside our main drive so with that I’m just going to click okay and then we’re going to choose where we want to install this program so we’re going to make sure all these are ticked on and then I’m going to click next then I’m going to select where I want to install this now as you can see I have it inside my C drive but not inside my program file so I can just go and install it directly on the C drive so I’m just going to go and do that click next then I’m going to choose a language in this case it’s going to be English and then we can just go ahead and make it set up our program on our computer so it’s just going to unpack and install now if you do insist that you want to install this inside your program files then I do have a Link in the description where you can go in and actually make sure there’s no warnings popping up when you try to run this program inside the program files but like I said if you just install it directly inside the C drive like I did here we’re not going to have any sort of issues now once you have it installed it’s going to ask if you want to start the control panel now if you want to wait with later for now let’s just go ahead and not do that because I do want to show where exactly this is installed so you can just open it up from inside your computer so with that I’m going to click finish and then you’re going to go into a installed xamp which is inside in my case the C drive so I’m going to go into this PC inside my C drive then I’m going to go down to the bottom here and then you can see I have xamp inside the XM folder we’re going to have the actual server files which means that we can scroll down to the bottom and actually run this control panel that we were just asked about so we can just go and click the xamp dash contr control.exe open it up and then you can see we have a little software in here now the important thing for you to know about in here is that we have two services that we need in order to actually get PHP working one is going to be the Apache server which is the one that we need in order to actually run PHP and the second one is the MySQL server which is used in order to get access to our database so what I can do is I can start these two and then you can see we have them running another tip that I have for you is to make sure that you go down and actually dock this at the bottom since this is the program you’re going to have to start every single time you need to start working on your website this means that we need to go down and actually dock it or pin it to your taskbar so you have easy access to it next time with this running we now need to set up our website inside This Server here which is very easy to do so we’re going to go back inside our folder where we have XM installed and then you’re going to go up to the top here and then you’re going to go inside the folder called HT docs now in here you’re going to find a bunch of files and these are just mainly to welcome you into the XM software so if I were to go inside my browser here and inside the URL I’m going to type Local Host and then you can see we get this little website here and this is basically what we see with these files inside the HT docs folder this is basically what this is so we don’t really need to have this so what I can do is I can go back inside our folder and then I just go and delete all the files that we have in here now the important thing for you to know about this folder here is that this is going to be the place where you start creating your websites every time you want to create a new website inside This Server here so what we can do is we can go and create a new root folder so I’m going to right click and say I want to create a new folder I can call this one my website just to give it some kind of name of course you’re more than welcome to call whatever you want it to be but in my case I’m just going to call it my website and now what you’re going to notice is that inside the browser I can go back inside and type Local Host and then you can see we get a list of all the different websites that I have inside this folder here this means that if I were to create a second website go in here create a second one my second website then you can see if I were to refresh in here we now have a new website that we can open up using this server here so if you were to click my website you can now see that we have this website open so going inside your preferred editor my case this is going to be Visual Studio code I’m going to go ahead and create my first file which means that I’m going to save this file inside this folder that I just created a very good advice for you is to go inside and actually dock the HD docs folder on the side over here so have quick access to it so what I can do is I can go ahead and go inside find xamp take my HT docs folder and dock it over here on the side so in this sort of way I have quick access to it whenever I have to open my folders here so I can click it go in here let’s just go and delete that second website since we don’t actually need it I’m going to go inside my root folder and create a index.php now this is the moment where some people are going to get confused if it came directly from my HTML course because when it comes to phsp we want to make sure that instead of creating HTML files we create a PHP file the main difference here is that we actually allow for PHP to be run inside these files here you can still write HTML just like you can before so you don’t need to freak out about your website breaking or anything like that or not being able to write HTML inside these files just because it’s called PHP and the same thing goes if you have an existing website that you want to convert into a PHP website you can just take all the different HTML files that you have and just change the extension from HTML to PHP on those and it’s going to work inside your server and it’s not going to break anything by the way I should say that because some people do worry that it is going to break something so I have to say it with this file here I’m going to save it and then you can see we have this front page here so if I were to go back inside my website I can refresh my website and then you can see we get a completely blank page and that’s because right now we have the index file running inside our server now depending on the editor you’re using because in some cases the editor is just going to work straight away but if you are using visual studio code it may ask you something down here at the bottom it says cannot validate since a PHP installation could not be found this is a very typical thing when you have a new version of Visual Studio code so if you have not set up phsp already inside this software you are going to have to set it up manually inside this text editor here so what you can do if you were quick enough is to make sure you opened up the little link it gave you if not then we’re going to go up into file go down to preferences go inside your settings then you’re going to click on extensions and then you’re going to go down to PHP and from in here you can actually set it to where you want to have the executable path set up inside a Json file so where to click this you can now see that we have this one line called phsp Dov validate do executable path and this is where we need to set in the link for our PHP installation which is again inside the exent folder so if I were to go back inside the XM folder go back you can see we have a folder in here called PHP so I’m going to click it and then you can see we have a php.exe file down here at the bottom this is the one we need to link to inside this executable path inside Visual Studio code so what I can do is I can copy my path here go back inside paste it inside the double quote and then we’re going to write back slash php.exe now if you covered the path directly like I did here you want to make sure these are not back slashes but instead forward slashes otherwise you’re going to get an error message and once you did this you’re just going to go and save the file and then you can close it down and now we have it set up so that we can actually find the PHP version that we’re using once we start creating a PHP website so just to kind of test this out let’s go and start up a regular HTML website now I’m just going to go and zoom in for you so you can actually see what is going on here and what I’m going to do is I’m going to go inside the body tags and create a pair of PHP tags which we use in order to write PHP inside a website so what I can do is I can write angle bracket question mark PHP question mark angle bracket and then anything that goes in between here is going to be considered as PHP so just to follow a very popular tradition here let’s go ahead and go inside and write Echo double quotes Hello World close it off with a semic on save it go inside our website refresh it and then you can see we get hello world so with that we now know that we have a server running so we can actually write PHP code inside our website and have it display inside the browser and with that in the next video we’re going to talk a bit about PHP syntax so we can actually write PHP properly inside our website so hope you enjoyed and I’ll see you in the next video [Music] now before we get started we need to talk a bit about the syntax of writing PP code since syntax is what is going to allow for you to write PHP code without having to create too many errors so in the last episode we talked a bit about opening and closing TX when it came to writing PHP inside a document like for example your index. PHP file and the important thing to know about here is that whenever you create these opening and closing tags your PHP is going to be parsing this page and search for these open open and closing tags until it finds one and then it’s going to see that phsp code inside those tags we can now very easily just take our phsp code and embedded directly inside our HTML like I did on screen here so you can just have the body tags you can have for example a paragraph tag and then right on theath you can just include some PHP code so it is very important that anytime you want to create PHP code you need to have these tags so you need to memorize these tags since we have to use them constantly whenever we want to create any sort of PHP code so when it comes to writing PHP code we do also need to talk about ending of each statement with a semicolon since semicolons is what is going to tell our code that this is a finished statement so as you can see inside my code here I have a very basic pair of PHP text and a echo which we haven’t talked about yet but essentially a echo is something we use in order to Output things inside our browser so if I want to Output some text or a string as we call it then I can Echo a string which is wrapped in double quotes and then end it off with a semicolon to tell it that okay so this is the end of the statement go ahead and Echo this out inside our browser but something you may not know is that the closing PHP tag actually automatically implies a semicolon which means that if I were to take an example like this one where we have two Echoes that Echoes out some code then the last statement doesn’t actually have a semicolon because that is actually implied by the closing PHP tag at the very bottom and I just want to point something out here because even though technically we don’t need to have a semicolon inside the last statement it is something that I do recommend that you do every single time it is also something most people do and it’s just for the simple reason that it doesn’t hurt anything to put that last semicolon and it also teaches you that every single time you create a statement you have to put a semicolon because a lot of times one of the errors that people they type in my comments is when they forgot to put a semicolon or they forgot to close off a parentheses or a curly bracket or something so teaching you the mindset of putting semicolons after each statement is something I highly recommend you do because you have to get into that mindset but now let’s talk about when we have a file that only has PHP inside of it because up until now we talked about this page for example the index a PHP file but in some cases we do also have files that are purely phsp the way we do it when we have this pure phsp file is you want to make sure you have the opening tag at the very top of the page every single time because otherwise your PHP is not going to be working but when it comes to the closing tag we actually want to omit it we don’t want to have a closing tag at the end this is actually the recommended thing to do so just like with the example you see here we have this file that only has a PHP tag at the very top but there’s no closing tag at the bottom it is for the simple reason that in some cases if you were to close off the PHP tag at the very bottom but then accidentally leave a empty line or a space or something then things can go a little bit wrong having talked about that let’s talk about a more advanced example of embedding PHP inside HTML in this example here you can see that I have a pair of body tags so we have some HTML and inside these body tags I have a PHP statement this is called a condition and conditions is something we will talk more about in the future so you don’t really need to know what a condition is right now but essentially I have a condition here where if something is true then run the blocker code inside the curly brackets and in between these curly brackets I did just like before I Echo out or output some text inside the browser or a string as we call it and as you can see I actually included some HTML tags inside that string so we have some html text but with a paragraph tag wrapped around it and this is something you can do whenever you want to create HTML inside a web page you can actually Echo it out using PHP so you can write HTML and content using phsp in this sort of way um but this is not really the most optimal way to do things because you may notice a couple of things here first of all the text is completely orange and that’s the typical color when it comes to writing a string inside phsp and because of that we run into some issues with the HTML not actually having any sort of syntax checking we don’t have any coloring of the HML just like the body tag up and below so writing HML like this inside H string is something that is going to get quite messy and it’s going to get confusing and you don’t really have any automated syntax checking cuz it’s not seen as HTML by your editor is seen as a PHP string so what you can do instead is you can split up your condition using the opening and closing PHP tags around the beginning of the statement and the closing of the statement so on the next slide here you’re going to notice that the if statement is going to get moved up next to the opening PHP tag and then I’m going to close it right after that line and then the curly bracket at the bottom there is going to have a opening and a closing PHP tag as well because by doing that we now allow for HML to be written in between those curly brackets but we can actually write it as HTML and the editor is also going to see it as HTML and actually check it for syntax and that kind of thing and color it so it looks really pretty so doing it that way is really the optimal way to do it I think when it comes to writing HTML the last thing I want to talk a bit about here is writing comments inside your PHP you have seen some of it already but I just want to just sort of like go through it since there’s a little bit more to it whenever you create phsp code write comments because at some point you’re going to forget what the code does and you have to return to it and you have to go through the code and see what it does when you could just have created a comment early on to tell future you what exactly the code does so creating comment is a very important thing a a comment is not going to get outputed inside the browser it is just there for you as the developer to see so we have talked about creating a single line comment here using two forward slashes and because this is a oneline comment we can’t go down to the next line and continue writing then it’s going to see it as not a comment uh but we can create multiple line comments by instead of using two forward slashes we can use a forward slash and a multiplication symbol and then close it off again using multiplication forward slash because in this sort of way we can now create multiple lines in between these two opening and closing tags when it comes to writing a comment and just like that you now know the basics of syntax when it comes to writing PHP there is of course you know more advanced things we could go into but I think this is a good beginning to understanding how to write phsp and not get any sort of basic errors inside your browser whenever you try to create any sort of basic phsp code so with that said I hope you enjoyed this little video and I’ll see you in the next one [Music] so in the last video we talked a bit about PHP syntax and today we’re going to talk a bit about how to create variables and data types now I do think it’s a really good idea that we go inside our edit on this video and just talk a bit about this so we have some practical experience writing PHP code and with that we do need to actually start up our website and I thought why not do that together since that is something we haven’t done yet so what you need to make sure you do every single time time you want to see your website inside the browser is first of all you need to have your editor open which is step one then you need to make sure you’re opening up examp so you want to open up the software make sure you actually start the Apache and MySQL server then you want to go inside your browser and then you want to go into the URL and type Local Host and when you do that you can see all the websites that you have inside your HD docs folder in my case here I do have two websites you should not have two you should only have one so go and pick the one that we created together in the first episode and then you can see we have our website in front of us here and that’s the basic step in order to open up your website when you’re running it inside a server like examp so now we can go back inside our index.php and just to kind of talk briefly about the last episode because I know some people were little bit confused when it came to embedding HTML and PHP together so if I were to go inside I can create a regular HTML paragraph and I can go and create this is a paragraph and just write some text inside this paragraph here now what you can do is you can create the PHP tag so the opening and closing tag so angle brackets question mark PHP and then you can say question mark angle bracket so what I could do is echo which we talked about is how to Output things inside the browser I can say this is also a paragraph and we do need to make sure we remember that semicolon even though because this is the last statement inside this particular PHP tag here we don’t technically need to have this last semicolon but like I said it is best practice to remember to do it every single time because it doesn’t hurt anything to do it so let’s just go and do it every single time and when you do this you can go back inside your website refresh it and then we can see we have two pieces of text we have this is a paragraph and this is also a paragraph So we have two paragraphs inside our website and just to show something a little bit more complicated you can also go inside existing HTML elements and create PHP so I can go in between here and I can open up my PHP tags and then I can write something additionally in here so we would have to again because we need to make sure to Output whatever we’re doing using PHP and I can go ahead and Echo awesome paragraph semicolon save this one go back inside my website and then you can see we included this awesome paragraph in here and this is just to kind of show that we can mix and match PHP and HTML together in any sort of way that we want by embedding it directly inside the HTML but now this is not what I wanted to talk about in this video here I do actually want to talk about variables and data types because we do have variables and data types which we use constantly when it comes to writing phsp code so this is something we have to memorize so let’s go and talk a bit about what exactly a variable is now a variable is when we have a memory location inside our application that stores some sort of data which is a very confusing way for beginners to understand what exactly a variable is because a lot of people don’t understand what is memory location and that kind of thing so instead just for practice here let’s pretend that a variable is a box and that box box has a label so you know when you’re moving into a new house you put you know some writing on the box whether it’s kitchen utensils or uh for the the kids room or something so you know what’s inside the box but then you do also have something inside the Box some actual data so whenever you grab this box by referring to the name of the box then you grab whatever is inside the box as well and that is technically what a variable does so when you reference to a variable you grab the data and it’s just a very easy way for us to refer to data and label it so we know exactly what what the data is now when it comes to PHP uh the way you refer to a variable or the way you declare a variable is by creating a dollar sign so if I were to go inside my PHP tags here I can create a dollar sign which means that now we are declaring a variable and then we can call it something so we can come up with a name for this variable that we think makes sense to the data that is inside the variable so in this case here I could say that this is a name so I could say that this is a name and it’s going to be equal to some kind of data so now we’re initializing this variable by assigning a piece of data to it so in this case here I could say that it is a string which we talked about before called Danny Crossing so now every time I refer to this variable called name I’m going to get a value called Danny Crossing so if I were to go below here and just echo which means that we’re outputting something inside the browser and I can go ahead and Echo out name or the variable called name so we need to remember the dollar sign here if I were to do this go back inside my website you can see that we’re echoing out Denny cing so in this sort of way we can create boxes or labels for pieces of data that we can refer to in order to better handle data inside our code so in this case here we created a variable called name and you can call whatever you want but now we do also have naming conventions when it comes to naming these variables here so in this case you can see we call this one variable name but we can also go in and either start it with a letter or a underscore in order to name this variable now the customary thing is to start with a non-capital Iz letter and then make sure that any other new words inside this variable name starts with a capitalized letter so if I were to write full name instead then we start with a non-c capitalized F and then a capitalized n for name so whenever you have multiple words inside the variable name then you just make sure the first letter of that second word or third word or fourth word is going to be capitalized and again it’s not really a customary thing you have to follow but it’s it’s the way that people do it so I would recommend doing it the same way so people don’t misunderstand what your code does or why you named your variables in this way that other people don’t usually do it and just to mention it when it comes to any other letters that isn’t the first letter inside the variable name then you can also go ahead and create either underscores or you can create numbers so we can say one but make sure you stick within letters underscores or numbers when it comes to the variable name after the first letter inside the variable name so now let’s go and delete what we have here and talk a bit about data types because when it comes to data types inside PHP we have many different data types we’re not going to talk about all of them in this video here since some of them are a little bit more complex than others uh but we will talk about some of the base types that we have inside phsp the first one is going to be what is called Scala types and Scala basically just means that the variable contains one value to it and the first example of that would be we could create a variable that is called something like string and a string we did already talk about what kind of data that is so in this case here we could say uh Daniel so as we know already a string is a piece of text now we do also have numbers so I can go below here and I can create a integer or a int and I’m going to go and name this one a number so we can just write some sort of random number here and this is going to be a value for a integer notice that we’re not using double quotes around the number because if I were to do that then all of a sudden this is going to be a string so it’s not considered a number inside the PHP language anymore it is actually considered to be a piece of text just like a string up here and you can actually tell by the color that it actually changed it to text now we do also have something called a float which is something that we see happen a lot in many other programming languages which is essentially when you have a number that has decimal points so if we to write something like 25678 then this is going to be considered a float because it has decimal points and then we do also have one more scaler type which is called a Boolean now I’m just going to go and write bull even though it is spelled buoen we’re just going to write bull for short and this one is going to be a true or false statement so essentially is something true or is it false and the values for that is going to be false or it is going to be true now it is also important to mention here that if you were to have numbers in here and you were to run this as a Boolean then one is also going to return as true and zero is going to return as false but in most cases we do just use true or false when it comes to this type of data here and just because I talked about it in the last video let’s go and create a comment for this section here so you know exactly what this is so we’re going to create a oneline commment and I’m going to call this one scaler types now the next one we have is actually one that we’re not going to do too much with here in the beginning but we do also have a array type so we can actually go and create a common and call this one array type and essentially a array is when we have a variable that has multiple piece of data inside of it so just like Scala types which means contains one value in the case of an array we have multiple values inside one variable so so could for example create a variable called array and I can name this one equal to multiple pieces of data now we do have two different ways you can create an array one is by writing array parentheses semicolon and then you can add multiple pieces of data in here which could for example be a bunch of string so I can go in here and say we have one string which is called Daniel and then I can create a comma and then add a second string which could be Bella then I can add a third piece of data and I can just keep piling data on inside this array here so we can say feta which is also a string and then we could of course rename the array to something like names if that makes a little bit more sense to what exactly this piece of data has inside of it but essentially this is going to be a bunch of data inside one variable and just to mention it since I did mention there was another way to do this instead of creating array parentheses you can also do a square bracket and then close it off using a square bracket as well just like this now if you just started following this course here you should have a newer version of PHP but if you do run PHP 5.4 or lower then these square brackets are not going to work when it comes to writing a PHP code so if you run a older version of PHP then you do have to do it the way by creating this array around it but just like I said if you’re a little bit confused about why we use arrays inside our code and you may have questions about it don’t worry too much about it because we will get to talk more about arrays in the future for now like I said you’re only going to have to worry about these different scaler types here I do want to mention one more data type though even though it’s not one you should concern yourself with here at the beginning since this is something that’s a little bit further ahead in this course here but we do also have something called an object type so we can say object type and an object type is essentially just when we have a object that is equal to a variable objects is something we create based on classes which is something we again are not talking about right now but when we do instantiate a class we do actually create a variable that is equal to an object based off of that class so could for example say that we have a variable called object and then we set it equal to a new object which is going to be the name of the new object so we could for example say car parentheses and semicolon and this would instantiate a new car object which of course right now can’t find because we don’t have it but once we do have a class called card that we can instantiate then this is not going to throw an error message but like I said we’re not really going to worry too much about arrays and objects right now this early on in this course here so don’t worry if you get confused about because that is perfectly normal even though we’re not really supposed to talk about erasing objects yet I still thought it was important to just kind of like mention them because it is something that is very used inside PHP so knowing about them so you have kind of like a a little bell inside your head when we talk about objects in a future episode and you think to yourself oh wait I heard about that at some point inside this course here and then you might go back to this lesson here and think oh yeah we talked about objects in that early lesson oh now we get to talk about what exactly it is so don’t worry too much about right now it is something that you just need to have kind of like in the back of your head it’s not important right now the only thing you need to worry about right now is that we have these Scala types and when we actually create the variable we declare the variable and then we initialize it by assigning a value to it the reason I mentioned that is because it is possible to go down and create a variable and let’s just go and call it names and not assign anything to it and when you do this depending on the context of when you use this variable it is going to default to a certain value type so if you were to use this one in a string context then it will just go and say oh okay so this is supposed to be a string automatically and then it’s just going to assign a empty string to it and it will actually do something that looks like this so we just have an empty string that doesn’t have any sort of value inside of it and this is what it’s going to default too and the same thing goes for integers floats and booleans and arrays and objects they all default to something so in this case here if I were to go ahead let’s just go and use these up here so as a default a string defaults to nothing so just an empty string with double quote by the way an integer is going to default to zero and the same thing goes for floats they also default to zero and when it comes to booleans they default to faults and just to mention it here when it comes to an array so if we were to create a array because we did talk about them in this video so we do also need to just kind of mention this it is going to default to a empty pair of square brackets and when it comes to an object day default to null which means nothing null is not a concept that we’re going to talk about right now but it just basically means nothing however when it comes to declaring a variable you should always initialize it the reason I say this is because sometimes when we do create variables we don’t know what should be inside the variable just quite yet and in those cases we just declare a variable but we wait with assigning any sort of value to it and when those moments happen you should not do it this way because you do risk getting error mess messages inside your code so make sure that you always assign something to it by initializing the variable so with strings you put empty double quotes with a integer you put a zero the same thing with floats if you put a buo and always set it to false arrays are just going to have these square brackets here and objects are just going to be null so these are going to be the default values you should always put inside a variable if you don’t know what kind of data you should put inside of it just quite yet otherwise your interpreter is just going to throw you a warning in a lot of cases so so just go ahead and make that into a Happ and here at the end before we end up the episode I just want to say that it is perfectly normal to be completely overwhelmed with all this information here because this is a lot of information I’m dumping on people especially if this is your first programming language this is going to be completely new and this is going to be a lot of information I just want to say that is perfectly normal to be overwhelmed and in the future we will get to do many more practical examples where we do actually use variables for something and we use many different data types and it is something that is just going to be a bit more natural when you actually to start seeing how these are used in Practical examples and when you get that little oh okay Epiphany moment where okay so this is how we use these different things we’ve learned up until now then at that point you will remember things a lot easier okay so don’t be worried about if you can’t memorize all these things cuz no one expects you to memorize all these things in one sitting it is something that sticks with you as you start practicing PHP along the way just to give a short example here at the end just so people know exactly how we can use variables inside our code if I were to go back and sign up body tags here and at the very top I’m going to go ahead and declare a variable called name going to set this one equal to a string called Danny Crossing and what we can do here is we can go below and create a paragraph inside HTML then I can just go and say hi my name is and then we’re going to open our PHP tags close it off again comma and I’m learning PHP then what you’re going to do is you’re going to go up and grab your variable name and you’re going to go inside your PHP tags and you’re going to Echo out your variable name semicolon and when you do this and go inside the browser and refresh it you can see that now it says hi my name is Denny cing and I’m learning PHP so in this sort of way we can take data or variables and we can use them inside our code or inside our HTML if you want to do that to Output data in this sort of sense like this is a very basic example but just to kind of show that we can use variables to reference to data that we assigned to variables and just to mention one more thing because something we can also do is we can go down and say I want to create a new variable and I just go and call this one something like test just to give it some kind of name I can assign equal to name so in this case here we have a variable that is assigned equal to a variable and I were to take this name and copy it down instead of the echo you’ll now notice that we still do get hi my name is Danny Crossing and I’m learning PHP and that is because this first variable here has a piece of data assigned to it and then the second variable down here has that same variable which has data inside of it assigned to itself so we can also assign variables equal to variables in this sort of way here and with that we now know a lot about variables so I hope you enjoyed this episode here and I’ll see you in the next [Music] [Music] video so in the last video we talked talk a bit about variables and data types and in this video we’re going to talk a bit about predefined variables or what you also call built-in variables now a predefined variable is a variable that exist inside the PHP language so in comparison to a variable from the last episode where we created it ourselves by saying we have a variable by saying a dollar sign and then we give it some kind of name so we could for example say name set it equal to some kind of value like Daniel and then we have a variable but now this is a userdefined variable which means that we created it ourselves but we do also have variables inside the PHP language and these are called super globals which means that we can access these variables from anywhere inside our code no matter what the scope is inside our code now scope is something we haven’t talked about yet but it is something we’ll get a little bit more into once we start talking about functions in PHP so for now just know that super globals can be accessed from anywhere inside our code now we do have a list of super globals that we can gain access to and each of these do something different when it comes to grabbing these variables so I’m just going to go ahead and list them out here one by one and talk a bit about them and explain what exactly they do and what you can use them for inside your code and just keep in mind once we do this I don’t expect you to memorize all of these in the first go we will actually get to use more of them as we start you know continuing these lessons here so you will get more practical examples to just kind of help you understand how we use these but for now I’m just going to give a short description of what exactly they do and then we’ll talk about more of them in the future episodes now the first you need to know is that whenever we want to define a super Global or a predefined variable is that we reference them by creating the dollar sign but then we create a underscore followed by a capitalized word so for example we could access the server variable and you want to make sure you add these square brackets afterwards and then semicolon so we could for example go inside and say we want to get the documentor root which is going to give us information about the root path of this particular website here so in order to access it inside the browser would do of course need to Output it by echoing it out so if we were to go inside my website refresh it you can now see that we get the C drive xamp HT docks which funny enough is the folder that we talked about in the first episode where we installed xamp so this is the location of the website inside our computer so what I could also do is I can go underneath here and just sort of copy paste this and I could also get some information about the PHP unor self then go inside the browser refresh it and then we can see we get some more information here so now we get the my website which is the name of the root folder that we’re inside of right now and we also get the name of the file that we’re inside of right now and the reason these are written right next to each other is of course because we have them echoed right after each other we could also go down below here and say we want to Echo out a HTML break which is if you know HTML which you should know by now is how to create a new line so want to save this go inside you can now see that these are the two different pieces of information that we get using the server super Global and there’s many pieces of information you can get about the server and there’s a huge list that I will include inside the documentation inside the description of this video here but for now we just going to take a couple of examples here just to kind of show you a little bit about what I can do uh the next one I want to show you is going to be the server name so if I were to go down and copy paste the break I can also go in here and grab the server uncore name if I were to do that you can now see that we get local host and that is of course because we’re working on a local server right now so if if you had your website on a online server that would be the name of that server we can also copy paste one more time and we can do one called request method underscore method and what this will do is tell you how this page was access so in this case here you could for example say that this is using a get method but if you were to access a page using another method like a post method then you can also see that when you output what kind of request method you were using now if you come from the HML CSS course that I have on my channel you may have heard something about get and post methods and that is essentially when you have a form inside a website you know just a regular form you can fill in using HTML and inside the form we have two attributes we have a action and we have a method and inside the method you just simply state if you want to submit the data using a get or a post method and that is essentially the information we’re getting here in a couple of episodes from now we will do a exercise together where we will be using this particular method in order to graph some data so in a couple of episodes from now do take note that we have a dollar signore server request method since we will be using that particular one in a future episode now the next one we’re going to talk about is going to be dollar signore getet which is another Super Global that you may have some bells ringing in your head about because we just talked about get and post methods so essentially when it comes to handling data and submitting data from page to page inside our website we can do so using a get or post method like I said using a HTML form so when we do that we can use a get or post method in order to grab that data from inside the URL which means that I can go in and instead of all this stuff that we just wrote here I could go in and say that we want to grab a piece of data so dollar signore get square brackets semicolon and I want to grab a piece of data that might have a label labeled as name now currently we don’t actually have anything like this inside our website but what you could do is you can go inside your website go go inside the URL and say that we have our current page which is index.php and then you can add a question mark name equal to Danny and because we have this name equal to Danny inside our URL after the question mark we’re now accessing a piece of data inside the URL which could have been submitted by a get method so this is how it would actually look like if you were to submit a piece of data from a form using HTML so if I were to go inside my code and actually Echo this out because like we talked about we do need to Echo in order to output something inside the browser if I were to do this because I have this inside the URL and refresh it you can now see we get Danny and that is because right now all the data inside the URL is going to be accessed as an associative array which means that we have a bunch of data with labels that we can access using this get method here so if I were to go in here so what I could also do is I could add a ENT symbol which is the ant symbol and I could add in a second piece of data which in this case could be I color is equal to Blue so if we were to add in a second piece of data we could also go in and say we want to Echo out a piece of data which is called I color so if we wrote I color saved it refreshed it you can now see we get Denny blue using get methods is something we use quite often inside PHP since we do submit data from page to page constantly whenever we do something using phsp so knowing this is something that is very important for you to memorize however we do also have have a post method and that is going to work a little bit different because a post method will also submit data but a post method is not going to be visible inside the URL just like a get method is so essentially I could still have the same information submitted but we can’t see it inside the UR else so even though we might still have data accessible to us that we submitted to this page we can’t see it inside the browser which is very useful when it comes to submitting more sensitive data so in case someone is standing behind you and looking over your shoulder as you’re submitting data inside a website they can’t see it so it’s going to be more secretive when you submit it there’s of course many different benefits to using a post method over a get method it really depends on what kind of users you’re trying to get here the main rule of thumb is that if you’re trying to get data from a database or just get data that you want to show the user then we use a get method and if you want to submit data to the website or to a database inside the website then we use a post method a very good example of this is if you have a login system and you want to lock in the user when they’re typing in that login information and then submit the data then we don’t want that data to be seen so that has to be submitted using a post method but once they log in and they have this user profile page inside their website that they can visit then all the data inside the profile page could for example be grabed using a get method so again a couple of different ways to use these but now we do also have something called a request method so if we were to go down here and instead say request and do this and actually go back inside the URL and refresh it you can now see that we’re still getting Danny so even though I used a request method instead that still looks for the name label inside the URL we actually still get Danny and this is because a request method is going to be looking for get post and cookies when it comes to looking for data inside this website here the thing about using request though is that even though it is kind of like this super super Global where we can get both get methods and post methods and cookies and we just use one thing to get all three of these is that you don’t really know what you’re grabbing whenever the US to submit something so if I were to for example submit a form but also were to go inside and say that I want to just manually add some data inside the URL then if you don’t set it up properly and validate it properly and sanitize everything once they submit the data then it can kind of go in and do some security damage so the rule of thumb here is whenever you know that you’re just going to be handling post or get data just go ahead and use the get or post method instead so just kind of forget about this one for now just remember that we have a get and a post method and and just don’t look at this one for now the next one I want to talk about is going to be the files super Global so we have one called files now this one is going to be used whenever you want to get data about a file that has been uploaded to your server so in case where you have a HTML form again we’re talking about forms here inside HTML forms we can allow users to submit files when they actually want to submit their form so for example a picture or PDF document or something they want to upload to the website then they can do that using a HTML form and whenever a user does that we need to double check all sorts of things about the file once they actually upload the file to make sure that this file should be uploaded to our website because let’s say for example a user decides to crash our website by uploading a file that may be very very very large in file size then we want to have something to double check the file size of that file that the User submitted and we can do that using our super global call file since we get all sorts of information about the files that the User submitted for example the file size we can also get information about the name of the file uh what kind of extension it has is it a PDF file or is it a file format that we should not allow to be uploaded inside our website so using this super Global here just kind of like allow for us to grab information about files that the User submitted using a HTML form the next one I want to talk about is going to be one called dollar signore cookie now a cookie is essentially a small file that your server embeds on the users computer which means that we have a bunch of information we can store on the users computer and using this super Global here we can actually store or grab information about cookies inside our website and in the same sense we do also have one for a session so I can also grab a session variable so in this case here we could for example grab some information about um let me actually go and demonstrate this one so if we were to go some of PHP tags I could for example create a dollar signore session and inside the bracket I’m going to make sure to include a name for this session variable here so I could call this one username and if I were to go inside and store this username inside the browser using a session this could for example be a username called Crossing then I can access it by simply reference to this particular session variable that I just created called username then if I were to go inside the browser and refresh it even though we don’t have a session running we can actually gain access to this one because it’s inside the same page so right now you can see that we have this cring stored inside a session variable so we can store information about the user inside this session which is on the server side this means that if I were to close down the browser and actually close down the session we are running inside the website it is going to forget about the session variable unless I set it again inside the website which I do actually do right here because it’s not the same page and again we will get to talk much more about session variables and cookies in the future episode for now just know that we have these super Global session and cookie variables that we can use in order to grab data from inside a session or inside a cookie and the last one we have that I just want to mention here which we should definitely not get into right now because this is heavy stuff for a beginner uh but we do also have something called a EnV which is a environment variable that we can gain access to inside our PHP code environment variables are essentially very sensitive data that you want to have inside the particular environment that the user is working in so you know data that should not be accessible to either the user or other environments um but this is not something we’re going to talk about right now this is something we’ll have for a later episode and just so you have all of them right in front of you so you can see how they all look like these are all the super globals that we have inside phsp again it’s very important for you to know that I don’t expect you to memorize all these especially since we haven’t actually used these in any sort of practical examples inside our code so far but the reason I wanted to discuss super globals is because right now we will get to talk a bit about a couple of these in the upcoming lessons so instead of telling you that oh by the we have something called super globals and we’re just going to use two of them right now I thought it was a really good idea just to introduce you to all the super globals that we have so you know that they exist and what they do so in a future episode whenever we visit some sort of lesson that is related to for example sessions inside our tutorial then you know that there’s something called a session super Global because we talked about it so it is important that you know that these exist but I don’t expect you to memorize any of these just quite yet in the next episode we’re going to talk a bit about operators inside pH P which is something that isn’t quite complicated it’s it it’s pretty simple but it is the last lesson that we need in order to do a practical little exercise together where we build a calculator together since this is kind of like a tradition on my channel that we build a calculator using whatever programming language we’re using right now and when we do get to that episode we will talk about the server and the get super Global since we have to use those in order to do our calculator exercise which is why I wanted to talk about these right now since we have to use them so why not introduce them to you this early on and before we end off the episode here I just want to mention that there is another Global that we haven’t talked about just called Global which is a way for us to gain access to variables that we created from any sort of scope inside our code and the reason I didn’t mention it is because we will get to talk more about Scopes once we get to our function episode which is not far from now but once we do get to talk about creating functions we do need to talk about something called the local and the global scope inside our code and that particular super Global is relevant when we come to that particular episode so I will talk about that once we get to that episode there so with that said I hope you enjoyed this video and I’ll see you in the next [Music] [Music] one so now that we know how to create a variable and a super Global we can now talk about how to create a form inside our website and actually submit data that we can grab using PHP and do something with it using HTML forms together with PHP is something we do quite frequently with PHP and it is one of the main things that we actually use PHP for when it comes to handling any sort of data inside a website so if you don’t know how to create a HTML form I do have a very thorough HTML tutorial that does talk about how to create a HTML form that I will link in the description if you have the need for it we have to remember that this is a PHP course so talking about too much HTML and CSS is something that my subscribers actually told me not to do so if you don’t know how to create a HTML form and you want to know the specifics of creating a HTML form then watch that tutorial inside the description with that said I do have a form inside my index. PHP file here so as you can see I have a very basic main tag that has a form inside of it and inside this form I just simply have a input for the first name I have an input for the last name and I do also have a select which is a drop-down that allow you to pick your favorite pet type so just a basic form that allow for you to type some basic data inside the form and then submit it and just because I know some people don’t use labels inside their form do make sure that you use labels whenever you create a form since it allow for people with disabilities to better read your form so this is a important thing I do see some people use paragraph tags instead which I’ve done too in the past so I’m also at fault for doing that um but I’m also seeing people not use any tags whenever they create these labels for their form so just make sure that you use a label element when you want to create a form tag again this is not supposed to be an HTML tutorial and now I’m sitting here teaching HTML when it comes to a form we have talked about inside my HTML course that we do have a action and a method attribute inside the form tag and when it comes to these two different attributes here these are the ones that we use in order to tell our PHP how we want to submit the data and also where we want to submit the data too so in this case here you can see that I did actually tell it that I want to include my data and send sended to a PHP file called form handler. PHP which is inside my includes folder so as you can see inside my root directory I do also have a includes folder that I just simply created by creating a new folder and inside this form handler. PHP I have nothing inside of it so right now we have a clean file you could have called this anything you want so form Handler of test.php just something it doesn’t have to be form Handler it’s just kind of to tell us what exactly this is but in this case here I do have this empty PHP file so just to start with here let’s go ahead and open up our PHP tags and just like we talked about in my syntax video we do not want to include a closing tag because this particular file here is going to be a pure PHP file and when you do that it is best practice not to have a closing tag so now going back to the index of phsp file you can see that we have this post method that I set inside my form now we do have two different ways we can submit this data either using a post or a get method now a get method is going to actually submit the data inside the URL so you can see it whereas the post method is not going to show the data inside the browser so the general rule of thumb here is that whenever you’re submitting data and allowing the user to submit data then you want to use a post method and whenever you want to show something to the user inside a page then you use a get method so just kind of rule of thumb there but you know of course it’s not going to be in 100% of cases but in 98% of cases that is going to be how you’re going to do it and just to kind of show it here because I did talk talk about this in the last episode when we talked about super globals if you want to submit a form and send the data to the same page that you’re inside of there is a way to do it which is to go inside your action open up the PHP tags like so and then you just go in here and you do actually just include the server super Global that we talked about so we’re going to Echo out the server super Global and Target the phsp self so this is one way to do it just to mention it but with that in mind you do also need to do something else here so don’t just post this and use this because this is actually prone to hacking or xss which is called cross- site scripting uh so therefore you should not just post this just like it is right here uh so for now we’re just going to go and send it to a separate document which is how you do it pretty much most at a time I do see a lot of people in my comments they they want to know how to send data to the same page as the form is on which is of course you know in some cases you might find a use for it but in most cases you will be submitting the data to another page so in most cases is this is how you’re going to do it and now that I mention security here for a second because I know some PHP people will maybe point this out um whenever you have any sort of include files that are just pure PHP files you’re supposed to have it inside a private directory inside your server and that’s not how we’ve done it right now everything is public at the moment but we will talk more about Security in a future episode we’ll we talking about private folders and public folders and where to include certain phsp files and where should your HTML files be for now we’re practicing right we’re practicing PSP so this is how we’re going to have the directory right now so that was a lot of information that wasn’t really supposed to be included inside this lesson here but I thought it was important to talk about so I just wanted to mention those things so with that said as we talked about we have a action and a method now when we send this data to the other page we need to be able to grab it somehow and that’s something we need to talk about because inside your HL form all your different data or inputs should have a name attribute because because this is the reference name that we’re going to grab once we send the data to the next page whenever you grab the data using the name attribute you’re going to be grabbing whatever the user input so right now for example inside a text field uh whatever the user typed into the text field is what you’re going to be grabbing on to reference to for example first name but inside a select down here if you have a drop down the data that you’re going to be selecting when you reference to for example in this case here favorite pads is going to be the data that is inside the value attribute so again just a little bit of HTML knowledge there for the the non-html people who should know HTML by now but let’s go and talk about how to actually grab this data inside our form handler. PHP file so once I submit this form inside my website which by the way looks something like this if I fill in information for example Danny Crossing and then I choose a pet so in this case here I do actually have one of each of these types of pets and I don’t want to make any of them cry so I’m just going to select none for now cuz I’m a good dad once once I submit this it is going to send it to whatever I set inside the action attribute so going back inside our document here if I were to go inside the form Handler you can see we have nothing in here which means that if I were to actually submit this data inside the website you can see that we just get a blank page and burning eyes warning here a little bit late but the warning came so right now nothing is happening this is the exact same thing is just an empty page inside HTML or something like that so what we can do is we can go back inside our code and the first thing you want to do is you want to check if the user accessed this particular file in the proper way because it is possible to just go inside the URL inside your website and just go up here and type the address of that particular file that is inside the includes folder which by the way is also why we have private and public folders inside our directory which is something we’ll talk about later because those allow the user to not be able to access the private files just by going inside the URL however we always need to think in security whenever you do PHP always think security so the first thing we’re going to talk about here is going to be how to let the user not access the code if they didn’t access this file using the form that they had to submit the way we’re going to do that is using something called a condition which again we will have a more thorough tutorial on a little bit later but essentially a condition looks like this so we have a if statement that says if something is true then run the code inside these curly brackets that we have here which are these right here so so whatever condition you want to set inside this statement has to go inside the parentheses so if for example true then run this condition here which will always be true because true is true right um so what I can do is I can go inside of here and I can use one of the super globals that we talked about called server which I did mention that we had to memorize because we would be using it in a upcoming lesson which is going to be this one so we have this super Global here and what I want to check for is a request method so requestor method now just to kind of show you here because if I were to take this server super Global and let’s comment this out for now and go up here and use a method called V dump actually this is a buil-in function but if I were to use Vore dump which would actually output some data about this particular super Global inside the browser so we were to do this just to see whatever this is outputting I can go back inside the browser refresh it and then you can see we get string three get which we’re not supposed to be getting oh I forgot to set this one back to post so let’s go and do that for a second inside the form um so go back inside the website refresh it again and now you can see we get well we actually have to resubmit it so we go back again resubmit and now we get post so this basically tells us that we access this particular page using a post method which means that we can go back inside our code and say okay so if the user we just comment this V dump out because we don’t need it anymore if this user access this page using a request method that is equal to post then we allow for the code to be run inside these curly brackets here and this brings me to a good point because some people including myself in the past by the way have been doing this in a different way so instead of checking for a post method we would actually go in and instead check for a is set function which basically goes in and checks if something has been set currently so if were to go back inside my form and inside my button down here I could actually add a name attribute and set this name to submit which means that now if I were to submit this form I also submit a a post super Global that has submit inside of it so I could go back in here and check for a post super global Post and then we can check for brackets go in here and check for a submit so this is also a way to do it but it’s not considered to be the best way to do things so you should be using uh this method down here to do it so every single time you submit data to another page you want to run this condition because that has to be checked for every single time then once you’ve done that you go inside the condition here and then you want to grab the data and we can do that the same way that I just showed using the other if statement so that is by using a post super Global so we can create a variable which we talked about is kind of like a container and we can name it something like first name and I want to set it equal to some sort of data now in this case here I want to grab a post super Global which is the data that we sent to this page here and I can grab it by referencing to the name attribute inside the form so if I go back to the form I can actually go and delete this name attribute down here because we don’t actually need it and I can grab the first piece of data and this one has been set to first name so if I copy that go back inside my PHP and paste that in now I’m grabbing the data from the form however we’re not actually doing this in a very secure way we did talk about cross site scripting so if we were to go back inside my form here just go back again uh if I were to go inside this form you can actually write code into this form here and that is going to allow for users to hack your website or do certain things to your database that might destroy it inject JavaScript in into your website which is not a good thing so you want to make sure that you sanitize your data every single time the rule of thumb here never trust data that is submitted by a user which means that you always need to sanitize data that the user was able to submit so we go back inside our code here and what you want to do is you want to use a built-in function inside phsp which is called HTML special characters so what I can do is I can say HTML special characters and what you want to do is you want to grab grab the data so the post method here and you want to put it inside the parentheses of this particular buil-in function and now there’s a couple of parameters you could put behind this particular function here but for now this is pretty okay so we’re not going to do anything else what this function does is that it takes your data and it converts it into HTML entities which means that we can no longer inject code inside uh the fields that we posted inside our form those are going to get sanitized so we don’t see it as code but we just see it as HTML entities which which means it’s not going to be picked up as code a good example of this just to kind of demonstrate it if it were to go inside my index file I can go right above my form and I can create a HTML Ampersand so if I were to write this HTML entity and save it and go inside my browser you can see it’s going to be picked up as a Ampersand cuz that is the HTML entity for a Ampersand which means that if I were to actually go inside my form and write a Ampersand because it might be part of some code that I’m maliciously trying to inject into this website here to break it um then it’s not going to be seen as this symbol up here but instead it’s going to be seen as this right here which is definitely not some sort of JavaScript code so just to kind of talk a bit about what exactly that function does you know that’s what it does uh so you want to make sure you use this particular function every single time you grab data from a user to make sure they don’t inject any sort of malicious code into a website so we do have two more pieces of data so I want to just copy this down down and I want to change the next one to last name and then I want to make sure that we go inside the post method and we go back and check what is this one called it is called last name so we can go back inside here and copy that in the next one I can call uh pets or something and then we go back we check what did I call this one I called it favorite pet so I can post that in here and it is important to keep in mind here that the naming of the variables doesn’t matter you could call this one test but it wouldn’t be very descriptive so we have to make sure whenever we create a variable that we know what it does by describing what exactly it does so this one would be the first name this would be the last name and this would be whatever pets I submitted so this should technically probably be a little bit more descriptive favorite pet like so now with that said I do also just want to mention this we do also have another function so right now you can see they have HML special characters but we do also have one called HTML entities which almost does the same thing as HTML special characters but instead of just taking special characters and converting into a HTML entity HTML entities takes all applicable characters that you could use for example any sort of other non-code characters and it converts that into HTML entities as well but again in most cases we do just use HML special characters so just keep that in mind for now that we do have this one and I will of course leave documentation to that particular function if you want to check it out inside the description but just know that we will be using HMO Special Care characters in most cases now that we have the data we can start doing something to it so I could go down here and just do some sort of code so I could say I want to Echo out uh a string and I want to Echo out these are the data that the User submitted and then I can go down below and I could also Echo out a break just to get a HTML break so we can actually jump down to next line we could also written a PHP new line which would have been something like this this but let’s just go ahead and do a break uh so what I’ll do here is I’ll jump down to the next line and I want to Echo out a piece of data so in this case I want to grab my first name and I want to Echo that one out then I’m going to be copying these two lines and paste it below last name and then we want to write our favorite pet so just like so we can copy paste copy paste the favorite pet and with that we can now go back inside the browser and refresh the page just to reset everything and then type something else in so I could for example say Danny Crossing and then we could choose a pet let’s just go and choose a dog in this case here cuz Bess is sitting right there I don’t know if you can see him but he is just kind of sitting here at the back he’s a bit tired um but I could choose dog and submit this one and then you can see these are the data that the User submitted Denny cusing Dog so now we grab the data and we could actually Echo it out inside the page just to kind of show what data we grabbed from inside the form now of course in most cases you would not just be echoing out data but instead you would be going in here and actually doing something with the data so for example inserting it inside a database or run a certain function inside your website to to do something with the data but just to kind of show that this is where you would actually start doing things with the data so what you could also do is so we don’t get stuck inside this page because this is just meant for a page where we run phsp code that the user is not supposed to have anything to do with this page is only for us as a developer so what I’ll do is I’ll send the user back to our front page using a header function so I can go in here and say we want to set a location colon and then we want to set the location that we want to send the user to so in this case we want to go back One Directory so I’m going to sayt dot slash and then I want to go inside index.php so with this header function here we now run the code and once we get down to the last bit of code we now send the user back to the front page so if we were to do that go back inside the website let’s just go ahead and refresh it here if I were to submit this data you can now see that oh we went back inside the front page because we just ran the code inside the other page and then we get sent back again to the front page which by the way brings me to just another little security thing um if I were to go back in here we can also run a else statements which basically means that if this condition turns out to be false then instead of just getting stuck inside this page here I want to to send the user back to our front page so if the user got in here in some sort of weird way by not actually posting the form but they just went inside the URL and typed in the address for this page here then they still get sent back to the front page because they access this page illegitimately so including this down here just as a fail save is just kind of like a good thing to do with that said I do want to address one more thing that I often get comments about and I just want to just say this once and for all whenever you create any sort of error hand inside this script here that you created yourself for example if I were to go down here and let’s say I want to check if any of these has been left empty when the User submitted the form so they went inside the website and they did not fill in the first name they did not fill in the last name and then they submitted it then what should happen well of course we don’t want the user to be able to submit the form right cuz there’s no data to submit but we do want to require that they submit all the data and one way you can do that is going inside your phsp code so I can create another condition so I can say we have a if statement and inside this if statement I want to check for a method called empty so basically this one checks if a variable right now contains no data inside of it so if it’s empty essentially so I can take the first name and I can put it inside here and if this one returns true it means that there’s no data inside the variable which means the user did not submit a first name so what I could do is I go in here and I could say I want to exit the script because I don’t want the rest of the script to run I just want everything to stop right here and then I might want to send the user back to the front page so again we copy this header and we send the user back to the front page maybe with an arrow message or something so what people tell me is Daniel you silly little man you can just go inside your HTML form go inside the attribute for example inside this first input here and you can write required if you do that then the user cannot submit this form right I can’t tell you how long I’ve been waiting to gloat about this cuz people they keep telling me inside the comment section even though we have this required attribute you can still submit the form it is very important for me to point out that any sort of front end whether it being HTML CSS or JavaScript is not going to be good security let me demonstrate for you if I were to go inside my form Handler the PHP and just for now so we don’t accidentally exit anything or something like that and I’m just going to go ahead and delete all these header functions here because I I want to stay inside this page if something happens let’s just go and delete everything here uh so we stay inside this page and Echo out all the data once we submit the form so if I were to go inside my website and I refresh the browser right now we have a required attribute inside this form here so if I were to try and submit this without typing anything inside this first one I’m going to get this little error message here so it says please fill out this form right so we can’t possibly submit this right now because it’s telling me when I click it that I need to fill out the form however if you know a little bit about browsers you know that we do also have a depth tool built into every single browser at least every single mutton browser so what I can do is I can rightclick and I can inspect anything inside this website here so when I do that we get this little depth tool that opens up at the bottom here now let me just go and zoom in so you can actually see what is going on here so right now if I duck this one over on the right side so you can actually see uh you’ll notice that inside this dep tool we can see everything about the front end of our website which means that we cannot see any sort of PHP but we can see every single HTML CSS and JavaScript inside this web page here which means that we can actually change it I can go inside my input and as you can see it says required so I can just go ahead and delete that one and if I do that and now go back inside the website so I can close this down I can now submit the form even though I did not input anything inside the first input so it’s very important that you know that any sort of front end HTML CSS JavaScript or at least as long as it’s not backend JavaScript like for example node.js or something but any sort of front-end javascripts is not going to be any sort of security so always use server side security when it comes to security inside your website and a really good server side language to protect your website with is of course phsp because it runs in the server so any sort of time you do anything with phsp inside your website or handle any sort of data from the user inside your website you should always sanitize and run error handlers using PHP in order to check for any sort of thing that the user might do in order to try and hurt your website so that’s very important so with all that said this is the basics when it comes to submitting data using a HTML form and then doing something with the data using PHP so hope you enjoyed this lesson and I’ll see you guys in the next one [Music] today we’re going to talk a bit about operators inside PHP and operators is something we use all the time whenever we do anything inside PHP now essentially a operator is something that helps us with logic connecting data together or math or any sort of thing that has anything to do with operations inside code and when it comes to operators we have many different types and I’m just going to cover the most essential ones that I know we’re going to be using for the next many couple of lessons uh so we’re not going to cover all the operators that exist out there we will talk about the ones that you will be using most of the time operators is also something we’ll have to learn a little bit about in order to actually do our projects in the upcoming videos since we have to do that one calculator uh project that I promised but we can’t do without talking about operators first so we have to talk about how to do various kinds of operations the first kind we’re going to talk about is something called a string operator and a string operator is a way for us to conect connect Different Strings or just different pieces of data together inside one string so to speak so essentially let’s say I have two different variables I have variable a and I have variable B and if I want to connect these two together what we could do is we could create a variable C and say we want to set it equal to hello world but I already have this data somewhere else I already have it inside variable a and variable B so what I could just do instead is I could connect these two together to create one string so what I could do instead is I could actually go inside variable C and instead of just rewriting everything again because we already have the data so there’s no need to rewrite it right so what I can do is I can take variable a and I can say I want to connect these two together by writing a punctuation and then I can include variable B so in this sort of sense we can concatenate two pieces of data together by using this punctuation in order to say well I have this data and I have this data and I want to combine them using this punctuation here I do also want to mention that you should be leaving spaces so don’t do this but instead do this and if I were to actually go and Echo this out so if I go below here and say I want to Echo out variable C what I could do is just kind of take a look at how this looks like cuz it’s not going to look exactly like we think it is if I refresh it you can see we get hello world because we can catenated these two together um but we don’t have a space in between the words so how do we create a spacing between two pieces of data well the way we do that is by going in and say well okay so I I just concatenated two variables but what if I want to concatenate a string together with this so what I could do is I could for example say that I have a string and I want to create a space inside this string and then of course we need to concatenate the string with variable B by creating a punctuation so just like this we now concatenated a string in between these two variables so we created a small space here so yes this is a way to to create spaces between data by just concatenating a empty pair of double quotes uh so what we can do is we can go back in refresh it and then you can see we get that little bit of spacing there and this just kind of like a really neat way to you know connect two pieces of data together so we don’t have to recreate it again so we don’t have to rewrite hello world inside a new variable so we just use old data we have already and just combine it in here and with this we do also have something called arithmetic operators so if we were to go in and paste that in and delete what we have already a arithmetic operator is essentially math it’s just like you learned in elementary school I think you learned this kind of math so essentially like plus minus multiply division uh we do also have some other things like um we do also have something called modulo and exponentiation so we do also have some high school things added in here but what I could do is I go in here and just simply Echo out some data so I could say 1 + 2 and we’re going to Echo this out now this is actually just a arithmetic operator when we add two numbers together like we just did here so in this case here of course if we would to save this and go inside my browser you can see we going to we’re going to get three because 1 + 2 is equal to three and the same sense we can do with all sorts of operators so we can also go in and we can you know minus we can also go in and multiply we can also go in and divide if we want to do that uh but we do also have this called modulo which is essentially when we go in and we want to have the remainder of something specific so in this case here let’s actually go and take 10 and say we want to divide by three or not divide modular by three uh essentially what you’re doing here is you’re dividing 3 into 10 and when you can’t do it any further then you need to see how many numbers are left over in the end 3 6 9 and then we can’t do anymore right but we we got to nine which means in order to get to 10 we have one more left so this would actually equal one if we were to go back inside the browser and refresh it so as you can see we get one and then we of course do also have to the power of which is basically going in and writing two multiplication symbols so if we take 10 to the power of three if we were to go back inside and refresh the browser you can see we get a thand because 10 to the th to the power of three is a th000 so we can do you know basic math calculations here uh but we do also have something called U procedence which is something something that goes in and helps us a little bit when it comes to doing a little bit more complicated math because let’s take an example here I do actually have an example on the side here in this example here you would normally if you know math from back in you know like back in the days when you learned math in school you would know that in this case multiply always comes before plus and minus which means that we have to say 4 * 2 which is 8 + 1 which is 9 so if you were to go ahead and do this go back inside my browser refresh it you can see we get nine however if I want to change the procedence of what gets calculated first inside a arithmetic operator or just a basic uh mathematic equation I can use parentheses order to do so so if we were to go in here and say you know what I want plus to go first so in this case here I write parentheses around one + 2 which means that these two are going to get calculated together first before anything outside the parth gets calculated afterwards so 1 + 2 is 3 and then 3 + 4 is 12 took way too long for me to calculate but this should end up being 12 so if we go back inside the browser and refresh it you can see we get 12 so we do also have something called operator procedence whenever we use parentheses we can do calculations and we can use more than one parentheses so we would to do uh 4 minus 2 then I can also use parentheses around here and then of course uh once we calculate these two together we’re then afterwards going to calculate these two together and then we’re going to multiply at the end there so again you can use this many as you want with that said we do also have something called assignment operators and what we can do here is basically assign things to something else which means that if we were to go in and say uh variable a is equal to two so in this case here I just assigned two to variable a and it is important to note here that we do not say equal to two cuz there is a small difference between saying that it’s equal to something or that we assign something to something it’s not really that important to know but I I just thought I’d mention that it’s not equal to it’s that it gets assigned to because variable a is a space in the memory so if you assign a piece of data to that space in memory then it’s not the same as saying that it’s equal to basically we just assign data to um to variable a that’s that’s what you need to know here with that said this is how we can assign a piece of data to a variable which we have done plenty of times up until now but let’s say I want to do something a little bit different let’s say I do also want to say variable a is going to be equal to itself plus something else what you could do is you could go in and say variable a + 4 and this would actually work out this is variable a which is 2 + 4 and then it gets assigned back to variable a which means that now now variable a is going to be equal to 6 right however this is extra code and this is just not how we want to do things we’re essentially double writing variable a which uh is not really considered best practice so what we can do instead is we can just go ahead and delete and do something like this so we can say plus equal to 4 so whenever we use any sort of arithmetic operator which we talked about which is plus minus multiply divide because we could also do divide if we wanted to do that but whenever we do something like this we’re essentially saying go ahead and take variable a and set it equal to itself plus whatever is after the equal sign so in this case we would also still get six so if we were to go and Echo this out so we’re going to Echo out variable a then you can see we get six inside the browser and the same way like I said we can do with any sort of operators that we learned about previously like minutes ago uh so I could go in and say multiply so in this case here it’s 4 * 2 so I would to go back in you can see that we get eight but now let’s talk about the next operator type which is something called a comparison operator and this is something that you will be using very often whenever it comes to any sort of conditions inside your code uh we have talked a bit about conditions in the past you know when you have a if statement says if this code is true then run whatever code is inside this like the curly brackets below so to give an example here let’s go and delete what we have here and let’s go ahead and create a if statement we haven’t really talked about if statements in depth we will get to do that I believe in the next video but let’s just go and create an if statement essentially when you have an if statement like this with the if keyword whatever is inside the parentheses has to be true in order for the code inside the curly brackets to run but let’s go and create a couple of variables here just to to talk a bit about a comparison operator so let’s say we have variable a and variable a has a piece of data assigned to it which is two and I can also go ahead and say we have something called a variable B which is going to have something like four assigned to it now what I can do is I can go inside my parentheses here and I can say is variable a equal to variable B what you’ll notice is that I’m actually using two equal signs here so I’m not using one because this means that we’re assigning something to variable a so a is going to be equal or be assigned variable B um and that’s not the same thing as doing two equal signs when you do it like this you basically checking if two pieces of data are the same and we’re not really checking for data types here we’re just checking if they’re the same what I mean by that is if I were to actually let’s go and output something so let’s say if this is true then Echo out this statement is true just to Echo something out inside the browser so if we were to do this and go back inside Firefox here refresh it you can see we get nothing so far and that’s because of course these are not equal to each other two is not equal to four but let’s say I went down and changed this to two and went back inside my browser now they’re going to be equal to each other right cuz two is equal to two however if I go back in here and say what if variable B is not a number but let’s say this is a string like it’s still two but now it turned into a string so is this going to be equal to each other what is your guess cuz we’re going to go in and find out now it is still going to be true and that is because we’re not checking for data types in this case however if I go back down to the equal signs and write a third equal sign now we’re checking for if they’re true and if they’re the same data type so we’re taking for two things now so if we go back inside the browser refresh it here you can now see oh it’s not outputting anything and that’s because it’s not true because this right here is a string data type again we can go and remove this just to test this go back inside the browser and now it is going to be true so two equal signs means that we’re comparing two pieces of data and three means they we’re comparing two pieces of data but also if they’re the same data type with that said let’s go and go back to the first one so two equal signs means we’re just checking if they’re equal to each other right what I can also do is I can replace one of the equal signs or the first equal sign I should say with a exclamation mark if I do this then I’m checking if they’re not true so right now because 2 is equal to 2 this condition down here is actually going to turn out false so if I were to save this go back inside the browser you can see that we’re not going to get anything but if I were to go back inside my code and make this four again then they’re not equal to each other which means that this condition is going to be true so if we were to go back in refresh it you can now see that we’re going to get this statement is true and with that we do of course also have three equal signs we replace the first one with a exclamation mark so this is going to check if they’re not the same data type or if they’re not the same number and we can also do other things and just comparing if they’re equal to each other we can also go in and say what if one should be lesser than the other numbers so right now we’re checking is a lesser than b which in this case is going to be true so if we go back in you can see that we actually output this statement is true but if we were to change this one to a five so we have you know is five lesser than four this is going to be false so we were to go back in you can see we don’t output anything and just like you learned in school we can also take for other things so not just lesser them but also greater than or we can also check for lesser than and equal to the other piece of data so in this case here if 4 is equal to four then this is going to be true right cuz it’s lesser than or equal to B so we would to go back in refresh it you can see we get an output and just to show one last thing there is also another way of writing this right here so is not equal to each other we can also write like this which does the exact same thing we’re just checking if they’re not equal to each other and we’re not really caring about data type in most cases just to mention it I do this one it’s just the way that I think is easier for me to make it make sense so this is the one that I use and with these this is a perfect time to talk a bit about something called logical operators because logical operators whenever we go inside a if statement like this one down here let’s say right now I’m checking is a equal to B which in this case is going to be true because 4 is equal to 4 so if we were to go back in and actually refresh you can see that we get this statement is true now let’s say I want to check for more than just one condition what you could do is you could copy paste a condition and put it inside another condition and now all of a sudden we start doing something called nesting nesting is something that most people frown upon because it starts creating very messy code I personally have done it in the past and I I regret doing it because it looks extremely messy but essentially you want to try and avoid having as many conditions inside other conditions as you can like in some cases you can’t avoid it but of course you know if you can then you should try not to and one way we can do that is by going inside the original condition here and say okay so what if I want to check for something else as well let’s say I have another pair of variables and this one is going to be C and this one is going to be D and this one is going to be two and this one is going to be six what I can do is I can go inside my condition here and say I want to check if this condition is true so is a equal to B which right now is true right so we output something inside the browser but I also want to check is c equal to D so what I can do is I can write and is variable C equal to variable D so right now we’re checking for two different things so the first condition has to be true but also because we wrote and the second condition also has to be true so both of these have to be true whenever we use and so would to go and do this go back inside the browser refresh it you can see oh we don’t get any sort of output because one of them is not true but let’s say instead I don’t want to check if they’re both true but I instead just want to check that one of them is true what I can do instead is I can write something called or so if I write or we’re basically saying this has to be true or this has to be true and if one of them is true then I’ll put something inside the browser so in this sort of way you can use logical operators in order to you know perform multiple conditions or multiple pieces of logic inside the same condition and it is possible to chain as many of these behind each other as you want so you can also go ahead and say we want to check for a and and then we can check is variable a equal to variable C then we can also do that and we can change as many of these behind each other as we want I do want to mention something here though which is something you will see in most cases when it comes to people doing programming which is that people don’t really write or or and uh because we do have another way of writing these and even though this may not make sense to a lot of people why we choose to do this instead um that’s just kind of like how people do it instead of writing or what you can instead do is write two pipe symbols and this means the exact same thing as writing or and instead of writing and we can use two ENT symbols and these two mean the exact same thing as or or and now the pipe symbol one is is the reason why I think this is going to be a little bit annoying for most people because the pipe symbol is not really the easiest thing to figure out where is on your keyboard I’m using a ntic keyboard which means my layout is going to be different for Americans or other people around the world but this is called a pipe symbol so pipe as in PIP PE pipe this right here so my best suggestion is to Google this to figure out where it is on your keyboard layout if you want to figure out where it is in my case I have to hold down alt and then the button right behind my backspace button and that is going to create my pipe symbol but now let’s talk about something here because right now I just chained three of these together but how exactly do U operator procedence function when it comes to this just like when we talked about the parentheses around you know when we were doing math uh we could determine what was going to be happening first but in this case here what is happening first are we going to be running these two first because we’re checking that one of these has to be true or we running these two first and then running the other one like what exactly is going on here when we use a Ampersand or a pipe symbol if I were to save this go back inside my browser you’ll notice that we get this statement is true which means that right now if I were to go and check this this statement is true but none of these are going to be true which means that we’re going to take the pipe symbol and this is where the divider is so essentially we’re splitting apart these condition checks and we’re saying Okay so this over here has to be compared first which which means that both of these have to be true but in this case none of them are so this is going to fail right but then we do also have a pipe symbol that says well okay so even if this fails we still also have this other side to check for and if this one is true then we’re still going to print something out inside the browser so in this case here as you can see we printed out this statement is true because the pipe symbol is going to be the divider that checks with these different conditions here so the pipe symbol is going to be the last determiner of what exactly is true inside this condition check here but now we do also have one last one that I want to talk about which is called incrementing and decrementing operators and these are essentially a way for us to do something inside Loops most of the time we can use them outside Loops if you want to um I can actually demonstrate this so if I were to go ahead and say variable a is going to be equal to 1 if I then go below here I can also Echo out variable a just so we can see EX exactly what this looks like inside the browser and then you can see we get one because variable a is equal to one however we do have something called a increment which means that I can go ahead and say variable a and add a Plus+ in front of it which means that we’re adding one to variable a which means that if we were to go inside the browser we now get two so a increment is basically just a way for us to add one to a piece of data and I can also go ahead and do a minus minus and this case we’re going to subtract one so we want to go back inside the browser you can see that we’re going to get zero however we do also have something called variable A++ which is a little bit different because basically the order of when we add one to this variable here is going to be changed so in this case here when we had Plus+ we say that we add one to variable a and then we Echo it out whereas if we were to go ahead and do plus plus after variable a we want to say that we want to Echo out variable a and then we want to add one to it so we would have go and do this what do you think we’re going to get inside the browser we’re going to get one so even though inside our code this is going to be two we don’t actually Echo out two because we’re adding one to it after we output it inside the browser so it would to go below here and actually say Echo and say I want to Echo variable a one more time then the second Echo is actually going to be two and the same thing goes for minus minus of course we can also say minus minus and then we’re going to Output a which is going to be equal to one and then we’re going to subtract one to it which means that the second time we Echo it out it is going to be zero so want to go back inside the browser and do this you can see we get one and zero and why does this matter you might be asking well in some cases when we start talking about loops inside programming and Loops is something that we will get to talk about a little bit in the future but essentially a loop is a way for us to Output the same code multiple times inside the browser as long as a certain condition is still true so let’s say I want to Loop out something until a certain number is lesser than 10 then we’re going to add one to the number that we’re checking for every single Loop and then when we get to 10 then it’s going to stop looping because now the number is not lesser than 10 anymore again this may be a little bit confusing because we haven’t talked about loops yet so don’t worry too much about it if you don’t quite understand what I’m talking about here I just want you to know that we have something called plus plus and minus minus so if you want to add a number or subtract a number you can do it by writing plus plus or minus minus so with that said I hope you enjoyed this lesson here we will get to talk a bit about conditions in the next video which is something we have touched upon in this video here and in some of the previous ones with the if statement you go in you check for a condition and then you output something if that condition is true but there’s a little bit more when it comes to condition so we’ll get to talk about that in the next video so hope you enjoyed and I’ll see you in the next one [Music] today we’re going to learn about something called control structures inside PHP and a control structure is essentially a way for us to navigate the code in different directions so if we want something specific to happen depending on something else then we can leave the code elsewhere and do something else if that makes sense we have many different types of control structures we have conditions which we’re going to talk about today we have switches we have a new thing in PHP 8 called a match we do also have include so we can include other files into our code and then we do also have something called Loops inside our code to Output code multiple times inside our browsers so there’s many different kinds of control structures and we’re going to talk about a few of them today we’re not going to talk about all of them since there is a lot to talk about um but we will get to more of them as we continue this course here but for now we’re going to focus on something called a Edition a switch and we’re going to talk about the new match thing that we just got inside PHP 8 so now we have talked a bit about a if statement before because we have used it a couple of times up until now in the previous video so essentially what you do is inside your PHP code you go inside and create a if statement and inside this if statement we can write a condition inside the parentheses to only run the code inside the curly brackets here if that condition is true so it’s important to point out here that we’re not talking talking about if the result inside the condition returns as true but if the condition is true so if I were to go inside and say that we have a bullan let’s create a variable up here I’m just going to call it Bull and I’m going to set this one equal to true so we’re not actually checking what is assigned to this Boolean up here so if I were to go in and actually copy this and paste it in we’re not actually checking if it is returning as true I mean right now we are so this is actually going to run the code but we can also go in and check if it’s not true this is actually also a operator that we forgot to talk about in the previous video but essentially I’m going in and I’m checking if this statement is not true then I want to run the code inside the curly brackets so we’re not checking if this value up here is true we’re checking if the condition is true okay whatever we’re checking for has to be true I just want to make sure people are not confused about that so with that said uh what we can do is we can actually go in and we can create a variable so I’m going to create a variable called a I’m going to set this this one equal to one and then I’m going to create another variable and I’m going to set this one equal to four so what I can do inside my condition down here is I can go in and use one of the operators that we talked about in the previous episode where we actually compare some of these different data so I can go in and say you know what I want to check for variable a and see if it’s lesser than variable B so in this case I can go in say is variable a less than variable B if so then Echo out first condition is true so in this case if I were to go back inside my browser refresh it you can see we get first condition is true because it is in fact true one is less than four but let’s say I want to check for multiple conditions inside one check here let’s say there’s two things that needs to be true what I could do is I could go inside my if condition here and just copy paste another if condition and then we would say Okay first we check for this thing if that is true then we go inside the statement and then we check for this thing and then we output something else that is one way to do it but we do something what is called nesting which is not really looked upon that fondly when it comes to programming uh since this creates a very weird spaghetti um messy code and that’s not really what we want to do when we’re doing programming we want everything to be very neat and tidy so instead what we can do is we can go outside and we can use one of the operators that we talked about in the previous episode so I can go inside my if condition here and say you know what I also want to check for something else so if a is lesser than b and our Boolean is true then run the code inside the condition here so in this case it is going to run because our Boolean is set true up here and just remember just like before I can also check if my Boolean is false by going in and adding the exclamation mark to say I want to check for the opposite it is also possible to do something else which is to go in here and say is our Boolean equal to false or as it is right now if I want to check if this Boolean is true then we can also just say is it true and then this would also work but it’s just kind of a habit for programmers to you know look a little bit more professional and actually check for a Boolean if it’s true or if it’s equal to false so using the exclamation mark here is something you should get used to I think because it is just a shorter way to write things and it makes you a look a bit more professional now in this case it’s not actually going to run this condition because our Boolean is true and I’m checking if it’s false so let’s say I want to check for another thing if this one fails I want to jump down to another condition and check for a second thing what I can do is I can go down below and I can create something called a else if statement and an else if statement basically is just a chain that we chain behind our if statement and says okay so if the first if condition is not met then jump down and check for the next one in the chain list so what I can do down here I can actually go and say you know what let’s go and check for the same thing but this time I want to check if our Boolean is equal to true so in this case here we would actually output something else now don’t get too confused about my code jumping back up behind each other because you can write it like this or you can write it like this it doesn’t really matter but because of my plugins inside my my little text editor here uh it automatically jumps back when I save so don’t get too confused about that it’s a way for the text editor to tell us that this belongs together this is a chain so therefore it jump spec behind it which I think is a bit weird but that’s just kind of how my plugin works so what I’ll do is I’ll go inside side and I’ll copy paste my echo and I’ll say the second condition is true because this one would actually be true then so we were to save this go inside my browser you can see that now we get our second condition is true and when it comes to a else if statement I just want to point out that you can write it like this in two words or you can combine it into one word and this would do the exact same thing it is kind of a habit for PHP programmers to have it in one word but in pretty much every other language out there we we split it into two words so the way I prefer to do it is using two words as well so now when it comes to adding these extra conditions behind the first if statement whenever we want to add more we just simply go in and add another else if statement so we can just continue just pasting and pasting as many as we want to create this huge chain where we check for a new thing and the important thing to note here is that whenever you hit a certain condition and it returns as true so let’s say this second condition up here is actually true then all the other conditions below are not going to run so it’s going to stop right there and it’s just going to Output whatever is inside this condition and it’s going to stop everything else from being checked so in this case here where every single one of the conditions are pasted below are actually true it is only going to Output one thing inside the browser because like I said it’s just going to stop the rest from running but now let’s say we want to have a fail save what if all of these fails to get run inside the browser because all the conditions are actually false what we can do is we can add a default Behavior so if a to go below here I can add a else statement without parenthesis and then I can write some code inside of here so we can copy paste paste the echo in and say none of the conditions were true so we can actually do this last effort here in order to say that something has to be output even if none of these are actually returning as true so let’s go inside all of the elive statements and just add a exclamation mark just to kind of say that you know what all of these are going to you know essentially be false so we’re going to save this go inside the browser refresh it and then you can see none of the conditions were true right now it says None of the condition were true which is not right it’s not plural there we go but you kind of get the idea here so what I’ll do is I’ll actually go ahead and decrease these cuz these are a lot of else if statements I’m just going to go and do this let’s just go and change this one so it doesn’t you know fail so we have the second one is actually succeeding so again if I were to save this go inside the browser we now get the second condition is true so with this we now talked about these if else if else statements however we do also have something called a switch inside PHP now a switch is a little bit different I do tend to see a lot of people are confused about when you use a switch compared to using IF else if else statements because they kind of do a little bit of the same thing on the Surfers uh but let’s go and create a switch and then we’ll talk about uh how they’re different and when you should use one or the other so in this case here I’ll create a switch which is using the switch keyword parenthesis and curly brackets now inside the parentheses we want to include whatever data we want to check for so in this case here let’s just go ahead and check for variable a so I’ll paste it inside the condition here and inside the actual switch curly brackets we’re going to write a bunch of cases which is kind of the same thing as taking for all these if else if else statements down here so I’m going to go and write a case and then I’m going to say what should the value be of variable a and if that is the value then we’ll run this block of code here so I’m going to write the case keyword and then right after we have to tell it what we’re checking for so if variable a is equal to something specific then run the code inside this case here so in this case I’ll check if a is equal to one it’s important to note we’re not writing case one so like this is the first case and then the second one has to be case number two and and so on we’re checking for the actual value inside this case here so if this were to be a string let’s say we had Daniel in here then I could ALS be checking for a string called Daniel in this case here so what I’ll do is I’ll just go back so we had the number here and inside the case number one let’s go ahead and create a colon and then say whatever code is in here we’re going to run if a is equal to 1 so I could for example Echo out um the first case is correct and you can write as much code as you want in here so we can also write more code down here below if you want to do that and just keep writing code um um but the important thing is that once you’re done writing the code that you need to actually get run if that case is equal to one is that you can go down below and add a break to tell it that now we’re done this is the break point of the first case so all the code in between the break and the case up there so everything in between here is going to get run if this is equal to one and we can of course add more of these so we can just go below here and say you know we have a second case and now I want to check if the number is three for example then we can go inside and say this second case is correct and just like with else if statements down here we just keep pasting more cases behind it and check for different values right after the case keyword here until we get something that is correct but let’s say we run into a scenario where none of these are correct just like we did below here let’s say we you know we don’t have this lse statement uh but none of these conditions are true but we want to run some default code if none of them are actually correct what I can also do inside the switch statement is I can go down and I can write default and then I can include some code below here so we can actually Echo out something else so we can actually say none of the conditions were true we just copy paste it up here and this will be what gets echoed out if none of the cases above are actually going to return as true and now some of you may be asking okay so when do we use a switch compared to using a you know bunch of if statements because they’re pretty much doing the same thing right the difference here is that inside a switch statement we’re checking for the value of a certain you know piece of data so in this case here variable a whereas inside a if condition we can actually check for multiple things so we’re checking is a lesser than b and is Boolean equal to fult so we can change it and then even in the next else if statement we can check for something completely different whereas inside a switch statement we’re checking for one thing in all the different cases down here so if you want to check for one specific value and then depending on that value you want to run a different set of code then you can use a switch statement but if you want to run different types of conditions that checks with different things then you can use a if else if else statement and just to show you what exactly is going on here because right now let’s say I want to go in here and I want to check for the number two and then I want to also write another case here and I want to check for the number three what we can do let’s say third just to have everything being correct what I can do is I can actually recreate this condition down here to show you exactly what is going on with the switch up here so if you were to go down here you could say is variable a equal to one that is essentially the exact same thing as this first case up here so if I were to go down to the elsf statement I can now check is variable a equal to 2 then I can add another one behind it so we can actually check for the next one which is is variable a equal to three so just to kind of show this is what the switch is doing it it’s comparing the same data to different sets of results let’s go ahead and comment out the switch and also the if conditions down here and let’s go above the switch and create create a match now A match is a little bit different from a switch because inside a switch here we just basically have a block of code that deviates into different directions depending on a certain results so if variable a is equal to something specific then we run a different piece of code however when it comes to a match we actually have a variable so we can call this one results and then we set it equal to our match keyword parentheses and curly brackets now because this one is a little bit different than a switch state or a if else if else condition we do want to add a semicolon at the end here because this is the exact same thing as creating a variable to we call this one result and then we set it equal to some piece of data and then in this case here we would actually add a semicolon behind it so this should have a semicolon behind it too because it’s the exact same thing as going in here and then creating curly brackets because we still need to have that semicolon so with that semicolon what we can do is we can go in and say you know we’re going to check for a certain thing so if variable a is equal to something specific then we want to return a value into our variable result depending on variable a so I can go in here and I can say you know what if variable a is equal to one then we can go ahead and assign a value which is going to be variable a is equal to 1 and in this case here we’re not actually going to add a semicolon we’re actually going to add a comma because we’re creating a small list of items here uh so what I can do is I can copy paste this below and then I can say if the data is equal to two then I can assign the data called variable a is equal to 2 so in this sort of way we’re just adding a different piece of data to a variable depending on what the result is now it is important to note here as well that the last condition you’re checking for in here should have a comma behind it this is how it’s supposed to be done so you do want to make sure that is that comma behind every single condition so don’t go down here and add a third condition and then delete the comma because this is the last statement uh just go ahead and make sure you have a comma behind all of them I do also want to mention here that you can check for multiple pieces of data inside the same condition here so you can actually go in and say okay so if variable a is equal to 1 or it is equal to three or it is equal to 5 then I want to insert variable a is equal to 1 inside variable result so using a comma when we want to tell it what we’re checking for here then we can separate different conditions and then output the same data inside variable results so just to Output this inside the browser let’s go and go below here and Echo out variable result because in order to actually get something inside the browser we do need to Echo it so doing this and going back inside the browser you can now see that we get variable a is equal to one another thing that’s important to note here is that we’re doing a more strict comparison type when we do any sort of comparisons inside this match statement here what I mean by that is we’re actually taking for types as well so if I were to to check for variable a being 1 3 or five but variable a is equal to a string that is equal to one then this is not going to return the actual data in here so if we were to save this and go inside the browser you can see we get a error message that says uncut unhandled match error which is the default error message you’re going to get if none of the actual checks in there is returned as any sort of data and just to mention it here for the people who do need to see it it’s the same thing as going inside a condition and then checking for variable a being equal to one but we use two equal signs so this is a very loose comparison so if variable a is equal to one or a string called one then it doesn’t matter but if we use three equal signs then it’s going to be a strict comparison type which means we also need this to be the same data type but now you can also do a default output inside a met statement so if you want to do the same thing as inside the switch down here where we have a default or with the else statement inside the if conditions down here then we can do the same thing when it comes to a met statement by simply going in and writing default and then point to some kind of value so we can go ah and say want to point to a string and say none were a match so now we have something default in case none of these are actually true so right now because variable a is equal to a string which is call one then none of these are actually going to be true but we’re still going to get an output inside the browser because we have a default return and again now the big question is when do we use a match versus a switch versus a if condition it really depends on what exactly the purpose is of your code so if right here you want to have a piece of data assigned to a variable but you want to have a different result based on something else inside your code then you can do that very easily using a match statement so instead of having to do a switch statement down here where we assign a piece of data to a variable we created up here which is going to be a lot more code than simply doing this then you can use a match instead but if you just want your code to Branch out depending on One Piece data then you can do that using a switch statement just based on the value of one variable inside your code but if you want to do different kinds of checks where you want to switch it up every single condition you’re doing inside this chain then you can use a if else if else statement down here in order to do different kinds of checks inside your code so it’s just kind of nice to have different types of tools inside PHP to do different things that is somewhat the same thing you want to do but slightly different and of course just like with all the other episodes this is quite a lot of information so we will get to do more practical examples with this in the future and with that said I do also want to mention here that in the next video we’re going to learn how to build a calculator together using PHP so we’re going to have an actual calculator inside our website you know using HTML and CSS we can type numbers into it then you can submit it and then you can get some sort of calculation out of it so we’re actually going to build something using what we learned up until now which is going to be quite exciting cuz now we can see how we can use PHP to actually do things inside a website up until until now youve just kind of been Gathering puzzle pieces but you haven’t actually learned how to put them all together to build something so that is going to be quite exciting uh so with that said I hope you enjoyed this video and I’ll see you in the next [Music] [Music] one so now we finally came to the point where we have to start learning how to create something using PHP since we’ve learned a lot of things about until now but we haven’t really learned how to put all those things together and actually create something using PHP the exercise we’re going to be doing today is to build a calculator inside the browser so we can actually go in and type into numbers and then we can perform some kind of operation on those numbers so we can multiply plus minus divide those kind of things uh so we can actually do something so as you can see in front of me here I have a basic index to PHP file that doesn’t really do that much it’s just kind of like a basic setup so we have something inside the browser I do have have two different style sheets inside my project here I do have a reset. CSS and I do also have a main. CSS file Now using these two different stylesheets is not something that should be new to anyone in here because a reset stylesheet is something you should know about already and using a regular style sheet for styling HTML things inside the browser is also something you should know by now if you don’t know how to do CSS there is of course a HTML and CSS tutorial on my channel but you shouldn’t need it at this point but it’s there if you do need it I do want to point something out about the CSS though because I have been told by my subscribers over and over again not to include CSS inside my videos because they’re just taking up space so if you want access to my personal files for these lessons here for example for these CSS files then I do have them available to all the different YouTube members and patrons so you can just go ahead and click the link in the description if you want to become part of that with that said I should also say something else which I shouldn’t really have to say but I I still got to say it because I keep keep getting questions about it CSS has no effects on your PSP code you can make the calculator in however way you want using CSS and it’s going to work the exact same way as my calculator with my CSS in here if you get any sort of errors in this video it is not because of the CSS okay just so it’s said so with that out of the way let’s go ahe and talk a bit about how to actually create a calculator so right now we have this basic front page and what we want to do in here at least what I want to do with this exercise is I want to build a calculator and have it run inside the same page because typically the way we do things is that whenever we want to have any sort of input from a user we use a HTML form so we can actually create that now to begin with so if I go inside my body tag here I’m going to create a form just a basic form it doesn’t have to be fancy and inside this form you need to have a action attribute and we do also need to have a method since we do need to submit this data and tell it how we want to submit this data should it be a get method or a post method now a get method will actually show the data inside the URL when we send this data because when we send data using forms we use the URL in order to do so but using a post method would actually hide the data so we can’t see it inside the URL and for this exercise here we’re going to go and use a get method just to begin with here because I do want to demonstrate something once we get a little bit further so if I were to go in and actually say I want to use a get method which means we can see the data inside the URL and then go inside the action here now when I submit this form I want to stay on the same page because typically when we submit the data we send the data to another PHP file and then we have that file handle the data and do things to it and then we go back to the front page with some kind of results however in this video I want to just stay on the same page I want to calculate things inside this same page and just show the data inside the same page here so the way we’re going to do that is we can either leave this action empty or we can go in and include something called a server super Global that points to the same page that we’re on right now one of the benefits to doing it this way is if I were to go in and say I want to open up my phsp tags because we do need to add phsp in order to add a server super Global is I can go in here and I can say I want to reference to Dollar signore server and then inside the brackets I want to reference to PHP self and of course a semicolon so when when we do this it is going to send the data to the same page that we’re on right now so we can do something with the data inside this page here now it is important to note that whenever you want to have anything shown inside the browser using PHP you want to make sure you escape it using HTML special characters otherwise you can have users that inject code inside your browser which is not very good always think security when it comes to PSP so inside of here we’re going to go to Echo and we’re going to Echo out HTML special characters or just CS with an S behind it parenthesis and then I want to grab the server super Global and paste it inside the parentheses and I don’t want to paste in the semicolon it has to be outside the the parentheses so we have it like this right here uh so doing this is going to escape our server super Global so users can’t accidentally accidentally people will UNP purpose try to hurt your website using this particular action here if you don’t sanitize the output of your code there’s a couple of different ways you can sanitize data and it really depends on what purpose it has are you trying to Output HTML into the website so we actually show something inside the website or are you trying to sanitize data submitted by the user then we use something else so there is a couple of different ways we sanitize data using PHP and in this case here because we’re writing HTML that is being output inside the browser we have to use HTML special characters now let’s go ahead and make sure that we have everything just jumping down on a new line because because I want to make sure you can see everything so I don’t have to scroll sideways like this constantly so I’m just going to make sure to wrap everything and I’m going to go and grab the form closing tag and I’m going to move it down and inside the form closing tag we’re going to go and include a input because I want to make sure that the user can actually type in a number that they want to calculate inside this calculator here so this is going to be a number type I’m going to include a name attribute and this one is going to be set to num one and then I do also want to include a placeholder just so the user knows exactly what to type in so we’re going to say we have a placeholder and this one could be number one or just something something that you think makes sense so they know what to type in inside this input the next thing I want to include is not the second number but I want to include the operator so in this case here I could add a select which is a way for us to create a drop down inside a form so I can call this one operator and then I can go inside and delete this ID now just to mention it since this might be something people are not aware of but the name attribute inside all these different input so inside the select we have down here and inside the input we have up here are going to be the name that we have to reference in order to grab this data from the URL after we submit this data here so just so you know exactly why we have the name input In Here Also I did not include a label for all these different inputs technically if you want this to be readable and you know allow screen readers to perfectly understand what this form is you know in order to make usability as good as it can you should add labels in here I talk about that inside my HTML form episode inside my HTML course but in this episode we’re just going to do some basic PHP calculator we’re not going to worry too much about HTML conventions and stuff like that we’re just going to build something using PHP so inside of here inside the select I’m going to add a option and inside this option I’m going to go and give it a value which is going to be add so we can just say we want to add something here so inside the option for the symol we’re going to see inside the browser I’m going to add a plus just so we know that this is plus adding so to speak so what I can do is I can copy paste this down and say I want to add a minus and we can call this one subtract we could also have called the above one addition because that is probably how you should say it then I’ll copy paste it below and I’ll go in and say I want to multiply and we can also say multiply inside our value multiply then I’m going to paste it down and I want to say I want to divide in this case here so divide and we’re going to add a division symbol and then for the last one down here we do also need to make sure the user can type in a second number so we’re just going to copy paste our input from up here so I’m just going to paste it in and I want to rename everything so we have name set to number two and also I want to set the placeholder to number two so now we have everything in terms of input so we just need to add a button in order to be able to submit this inside the browser so I’m going to add a button and just simply going to call this one calculate so now we have everything that we need in order to have a form that can actually submit data so if we were to go inside the browser just so we can see exactly what we have you can now see that I can type in a number so I can type two and then I can say what kind of operation do I want to perform so in this case I could say do I want to plus minus multiply divide so I could say minus and then we can say number one so this would actually end up being one once we calculate but in this case here it’s not going to do anything because we don’t actually have any PHP code that does anything inside this thing so that’s going to be the next thing we’re going to worry about like I said in this video here we’re just going to focus on doing everything inside the same page here so below my form I’m going to go and open up my phsp tags because I want to just add in the PHP here and in between these PHP tags the first thing we need to do is we need to actually check did the user actually submit the form data correctly because we don’t want to run any phsp code if the user did not submit the form data correctly so what I’ll do is I’ll include a if condition go in here and say inside the parentheses I want to check for something very specific so in this case I want to check for a super Global called dollar signore server and I want to check for a certain request method so I can say I want to check for a request uncore method which is going to be set equal to a certain method so in this case I want to check for a get method now we will run into a small issue here but just for now let’s just leave this as it is because as a default whenever you load up a page inside the browser it will be as a default set to a get method so this will run the code no matter what which is not what we want we only want to run the code if we actually submitted the form so technically we do need to go in and actually set this to a post method and then up here we do also need to make sure when we submit the data we submit it as a post method so go ahead and change these but don’t refresh the browser because I had a point in including a get method and that is simply that if we were to go inside the URL inside the browser you can actually see because I did actually try to click calculate one time before we changed all of this so using a get method you can see we get all this data inside the URL so we get number one is equal to two and operator is equal to subtract and number two is equal to one so we do get a bunch of data inside the URL and this is my point about how we submit data pass it into the URL so we can grab it again and then do something with it however when we use a post method we actually don’t see the data inside the URL even though we do submit it so if I were to go back in here and actually resubmit everything so we want to make sure we save everything inside the index file go back inside and refresh the browser now if we were to go in and type one and let’s just say 1+ one calculate you can now see that we don’t have any sort of data inside the URL we only have index.php but there’s nothing behind it so the data is still there we just don’t see it so the next thing we need to do inside the PHP code after checking for a post submit is that we need to go in and actually grab the data so we can use it for something inside our code so the way we do that is by going in and say you want to create a variable I’m just going to call this one number one going to set it equal to Dollar signore poost brackets and then I want to grab the name of the input that we included inside the HTML form so in this case here the first number was called num one because if it were to go up here you can actually see that’s what we included inside the name attribute so going back down we can include the num one so we actually grab it however even though this is bare minimum for grabbing data from inside a post or a get method so if it had to be a get method we would actually do this instead however this is not seen as being secure let’s pretend that right now inside your website a user goes inside this form and decide to start typing JavaScript code or SQL for injecting into a database or something then all of a sudden we have a hacker under loose that can destroy our website and database and all that stuff inject malicious code into your site and we don’t want that to happen you should always sanitize data whenever you have any sort of data submitted by the user and this is very important to do you should never trust the users whenever you have anything the user can type something into because they will try to break a website we do have a couple of different ways we can sanitize data either using a filter uncore VAR or a filter uncore input and in this case we’re just going to go and use input since that is specifically meant for either post get or cookies uh whenever it comes to sanitizing data from those particular sources so instead of grabbing the data like this instead I’m going to use filter underscore input parenthesis and then inside the parentheses we need to include some data so in this case I need to tell it what kind of data are we trying to sanitize here in this case here we’re trying to sanitize a post method so I’m going to say we have a input uncore post then I need to feed it the actual name of the post method that I’m trying to sanitize so in this case here it is called num one we do need to make sure this is inside a string by the way and then we need to tell it how we want to sanitize it like what kind of method do we want to sanitize it with in this case here I want to make sure we sanitize a float because when you go inside this calculator here you can go in and type a float number so you can write a decimal point uh so what I’ll do is I’ll go in here and tell it that we have a filter underscore sanitize you can actually see we get a bunch of options popping up here the one I’m looking for is sanitize _ float so that’s the one I’m going to paste in here so just to show it here if I were to zoom out it is going to look like this on one line okay it might be a little bit small for you to see but this is how it’s going to look like so the next thing I’m going to do is I’m going to grab the second number so I’m going to copy everything here and paste it below and the first thing I’m going to do is change the variable name so now we’re grabbing the second number and then I want to go and grab the number that had a num two set to the name attribute inside the form now when it comes to the next one here which is the operator we do actually need to do things slightly different because if I were to go in here and actually say I want to grab the operator so we can say we have a operator name that we want to grab if I were to go in and actually say I want to sanitize a string because this is actually a string data type that we’re submitting with the plus minus multiply divide icons here uh it is actually giving us a small error message it is saying that filter sanitized string is depreciated which means that it is no longer recommended that you use it inside your code and this is actually something new that came with PHP 8 because if you were to go inside the documentation inside php’s website you can actually see that they don’t recommend that you use this anymore because there’s a little bit confusion about exactly what you’re trying to do with this method here so instead they do actually recommend that you used HTML special characters instead of using this particular filter input here so if I were to go in I can actually go and just delete everything and say I have this HTML special characters and then I want to go in and actually just grab the post super Global and reference to the operator name and simply do this of course we do also need to change the variable name so we want to make sure we say Operator just to make sure this is also uh changed I know this looks quite confusing in like multiple lines like this but if I were to zoom out you can see this is like how it would look like when when it’s not looking messy and confusing so now that we grabb the data and sanitized it the next thing we need to do is we need to run something called error handlers because error handlers is a way for us to go in and actually try to prevent the user from doing things they’re not supposed to let’s say for example I go inside the browser here and leave one of these inputs empty so I go down and I leave the first one empty and I try to submit this then you can see oh it got submitted but we don’t want that to happen because then that means that things are going to go wrong there’s nothing to calculate in here so what we need to do here is make sure that the user has to type something in into both of these inputs here and I can already feel people typing comments now because some people will tell me that Daniel you can just go inside the form inputs and add the required attribute then people cannot submit the form so let’s go and do that let’s go inside the first input here and let’s add a required attribute at the end here and let’s go and do that for the second one as well so we’re going to go down to number two and we’ll also going to paste it in down there so now technically we cannot go inside this form and submit it without typing something right so if we were to go inside the browser refresh it just go ahead and say yes to continue the submission and if I were to try and submit this without typing anything oh we get this little popup it says please fill out this field and clearly no matter how many times I click it it is not going to submit so this is security right we are now preventing people from submitting I clearly have a point here in case you couldn’t tell um so if I were to go inside the browser and you just know a little bit about browsers what you can do is you can right click inside the input inspect go inside this little developer tool go in and actually delete the required attributes so I can actually go and delete this one and I can also go in and delete the second one so I’m just going to click enter close it down and now I can actually submit it so this is not going to be a valid way of trying to not submit the form without filling in inputs always use PHP for security don’t do any sort of security with front-end languages like HTML CSS or JavaScript in the case of JavaScript you can do it if you’re using it as a server side language but don’t do it if it’s a front-end language okay always do security using a server language so what I’m going to do is I’m going to go back down to my code and I want to start including some error handlers we can actually write a comment here just so we know exactly what we’re doing so I’m going to go and type error handlers so I know exactly what this code does we can also copy this comment here go above and say grab data so we know exactly what this does here so let’s go and add from inputs just to be precise now when it comes to error handlers we can pretty much come up with as many things as we want so for example I could say I want to check for empty inputs because that should not be allowed I could also check for a maximum number of decimal points or if they didn’t type in a number but instead typed in a letter on the keyboard so we can check for many different things the first thing we’re going to do inside this ER Handler here create a Boolean because I want to actually have a Boolean that tells me is there any errors happening inside my error handlers here so what I can do is I can say we have a Boolean called errors and I’m going to set this one equal to false because right now there’s no errors happening inside this code here so for now it is just going to stay as false but if we were to go down and say you know what let’s go and check if the user left any inputs empty what I can do is I can run a if condition and in inside this condition here I can say I’m going to use a function built into PHP called empty and basically what this one does is that allow for me to paste in a variable so for example number one and it is going to tell me whether or not this variable has no data inside of it so right now if the user did actually submit something inside number one so they type something inside the first input here then it will actually have data inside of it but if they left it empty then it’s not going to have any sort of data so if this one is empty or because we learned about this operator in the previous episode if number two is also empty or because we have a third thing from the user which is our operator so I also want to check if the operator is empty then run some error code inside the curly brackets the first thing I want to do is I want to Output a message inside the browser so I want to actually paste in this little piece of text that I have here so just a basic Echo where we Echo out a string which is going to be a piece of text called fill in all fields and then I simply wrapped some paragraph tags around it the paragraph tags are only there because I wanted to style my error message inside the browser so it actually turned red so people could actually see it but you can style this in however way you want to inser your CSS file after doing this I want to go down to next line and I want to make sure that we now set our errors equal to true because now we do actually have an error so this should be true and this is basically all we have to do right here so what I can do now is I can check for a second error message the second one we’re going to check for here is going to be whether or not the number submitted is actually a number or is it a letter because if someone were to go in here right now you can oh I actually managed to again this kind of proves that even though you go inside your form up here and you say that oh it has to be a number sometimes it doesn’t quite work because I can’t type letters in here but clearly I just did so it didn’t quite catch it when I clicked on my keyboard also you can right click inspect go in autofill it with a you know a value or something again don’t use HTML for security okay it’s not going to be enough I have absolutely no idea why actually type something in here but it proves my point so I’m kind of happy it happened so what I’ll do is I’ll go down and I want to create another if condition so I want to say I have a if condition and inside this condition I want to check for something called is numeric so I can actually go and run a build-in function called is numeric and then I’m just going to go and paste in our number one then I want to include a or and then I also want to check for number two remember we’re only checking numbers right now so we don’t need to have the operator included in here but something else we do also need to pay attention to here is that right now I’m checking are these numbers and if so then run a error message and that’s not really what we’re trying to check for here we want to check if the these are not numbers right so either I can go in and say is this equal to false then do something or we can do it the professional way and go in and just write a exclamation mark in front of is numeric because this is checking is this false and the same thing of course we need to do to the second one over here so right now if these are not numbers then run a error message inside this condition here so I’ll just go and copy paste what we have up here and I’ll change the message so instead of fill in all Fields I’ll instead say only write numbers and paste that in instead at this point here you can include as many error handles as you want to have inside your code I’m just going to go and leave this for now otherwise this is going to be a very long episode so let’s just go and include these two error handlers here and say that’s that’s okay for now so after checking for errors I want to go down and I’m going to create another comment and I want to say calculate the numbers if no errors then what I want to do is I want to run a if condition and I want to go in and say if there were no errors then run this calculation inside the curly brackets so the way we do that is Again by checking if errors up here is equal to false because if it’s equal to false then it means we had no errors because we didn’t change it further down so now we actually want to run the calculation so if errors is equal to false by adding the exclamation mark in front of it after doing this we want to go inside the curly brackets and we want to actually check what kind of operation we need to do here did the user want to plus or minus or multiply or divide we don’t really know so we need to run a switch statement in order to determine what exactly they submitted so going in here we can create a switch say we want to add a pair of parentheses curly brackets and inside the switch condition I’m going to go and say we want to include the operator because the operator is going to decide what exactly we’re doing with these numbers here so I’m going to paste it in because that is the one I’m looking for and inside we’re going to go and include a couple of different cases so I want to say case number one is going to be if we have a string that is equal to add then we add a colon and then we add a break just for now then in between these two we want to actually add in what is supposed to happen so in this case we want to actually add the two numbers together so what I’ll do is I’ll say that we have a variable called value that we’re creating right now and I want to set this one equal to variable number one plus variable number two semicolon and this right here is going to be the first calculation that we do inside this switch statement so now we just copy paste it because we have a couple more we have three more we need to paste in so we’ll paste in like so and then we’re just going to go and change the cases so right now we have subtract and then we also have multiply and we have divide and just to mention it here these strings here are what we typed in inside the options inside the form so if we were to go up inside the form you can see that these are the ones that we included in here so that’s the one that we actually refer to inside these cases down here now we do also need to make sure we change the operation so this is going to be minus this is going to be multiply and the last one is going to be divide and we do also want to add in a default because if something happens and something goes wrong and we don’t actually have any sort of data inside the operator or something something it could happen something might happen Okay so you have to think about everything here what could go wrong at some point so let’s go and include a default which is going to be an echo and this Echo is simply going to Echo out a error message so I’m just going to copy paste what we have up here paste it in and then I’m just going to say something went wrong something went horribly wrong just to be a little bit dramatic here so now the last thing we need to do in order for this to technically work is to go after the switch statement and actually output the calculation that we just calculated here so what I’ll do is I’ll go below here and say we want to Echo and I want to Echo out a string which is going to be a paragraph because I want to actually have this styled inside my browser so I’m going to close off the paragraph as well then I also want to go in and add a class because I do actually want to style my text inside the browser so I’m going to say I have a class and now something is going to happen and you may have noticed this on the previous Echoes up here because if I were to go in because typically when we have a class that equal to something we need to have double Cotes right how however if I were to do that we’re actually canceling out the surrounding string double quote so we can’t really do that if we were to try and write something in here you can see oh that is not working out it’s actually giving me an error message so something we need to talk about here is the use of single quotes because you can either use a single or a double code for for example creating a string so I can actually go and do this instead so we just use a single quote or we can use a double quote and the same thing goes for HTML you can either use double quotes or you can use single quotes and by using single quotes and mixing it together with double quotes we don’t cancel out the string so to speak so I can actually go inside my class and I can say I have a class name set to celc dash results and in this simple sense we now have something that works okay so we’re not canceling out anything here so inside the I want to Output result is equal to space then I do actually want to add in my value which is right now up here so we have a value variable so what I’m going to do is I’m going to concatenate things so we could actually just so it’s a little bit easier for you to understand go in here and delete the last ending paragraph tag and after the double quote I’m going to concatenate my variable and then again I want to concatenate a string because we need to close off the paragraph so I’ll go ahead and say we have a closing paragraph graph and do something like this so now at this point here this is technically going to work however there is going to be something slightly off about this code here let’s say this switch statement here gets run and the default case down here is the one that is actually being used because something goes wrong and we don’t actually run any of these cases up here what is going to happen is we’re going to get an error message because variable value is never created we don’t actually create it inside the default value down here so we don’t actually have it when we reference to it inside the echo down here so it’s going to Output a error message so what I’m going to do is I’m going to go right before the switch statement and I’m going to declare the variable so we have it no matter what so we don’t get an error message inside the browser and now typically we would be able to just go and declare the variable like I just did here so we just have a variable but we don’t actually assign any sort of data to it however in some cases that is actually going to throw a error message for some people it is not actually going to work it’s going to have a reference error or something so what we want to make sure we do here is we want to actually assign something to it as a default which is going to be zero the reason I’m adding zero is because this is going to be a number or a float that has to be assigned to this value here so this is going to be a float data type that is assigned to Value once we actually start doing those calculations down there we did talk about this in the previous episode where we talked about data types so we create variables that don’t actually have any sort of data assigned to it and the fact that this is not something we should do inside PHP because in some cases it is actually going to throw an error message so we do need to make sure we assign a default value to a variable inside PHP again we can keep adding to this project here but for now this is going to be okay so we just want to declare the variable before the switch and everything should be okay so having saved this let’s actually go and test this out inside the browser so if I were to go inside my browser and type in number one actually we need to refresh everything first so that is important don’t worry about those error messages those are only there because I went into the developer tool previously and deleted the required attribute so don’t worry about those right now all the changes we made to the code will take effect now as we click the next time on the calculate button okay so don’t worry about those error messages right now so going inside I can go in and say 2 + 2 is going to be equal to 4 so it’s working yay but now if I were to go inside the form and try to submit it without having any sort of data typed into let’s say the first one up here then we’re going to get a well first of all please fill out this field but if we were to go in and actually remove the required attributes you can see we’re going to get our error messages so in this kind of way we can create a cool calculator inside our website using all sorts of PHP code that we’ve learned up until now so I can actually go in let’s go and test out multipli so we can say 10 * 2 is going to be 20 so we can do a bunch of different calculations and just kind of like experiment with it and and try out what we just created together here so with that this is how we can build a calculator using PHP I hope you enjoyed this lesson here because this is a stepping stone to learning PHP this is the moment where people are going to go and say oh that’s pretty awesome now we have hope that we can actually learn PHP and do something inside websites with it hey so this is a pretty big moment I think when it comes to learning PHP so with that said I hope you enjoyed and I’ll see you guys in the next video [Music] so back in our data types episode we talked a bit about arrays but we haven’t actually talk specifically about how we can create arrays and change them and what we use them for and how to create different arrays with different types of keys inside of them so there’s a little bit we need to talk about when it comes to array since an array is used very often inside PHP the previous episode where we talked about how to create a calculator where we did a small example together uh could have been done a little bit easier if we did use an array in order to Output error messages inside the browser so arrays is something we have to talk about now an array is a type of data structure that allow for us to store multiple values inside one variable so we can have one variable that is equal to many different types of data so we can easier manage many pieces of data at the same time so let’s be a little bit boring here and create a list of fruits because that is going to be a very good way to kind of show you the example of what I mean when I talk about storing many pieces of dat inside one variable because if I were to go down here and actually create a fruit variable and call it fruit one I can set it equal to a apple now the problem here is that if I want to have another piece of fruit and store that equal to another variable then I would have to go down and copy this down and say we have something called fruit two and set it equal to a pair and doing this is not really a good idea because all of a sudden we have all these different variables that could just have been combined into into one variable and then stor it in there so we could just access that one variable to grab all the different fruits inside my code so let’s say instead of doing all of this I’m going to go and delete back and I say I want to have a fruits variable now I’m going to turn this one into an array and there’s two different ways we can do that either I can go in and say we have an array data type parenthesis and then inside the parentheses I can go in and say I want to add a apple a banana yes that’s how I say banana I think you say banana like that might sound a little bit more correct so I’m going to go and add in a third piece of fruit which is going to be a cherry and now we have all three pieces of data inside one variable here inside an array now there is another way to write this though which is a little bit shorter and in my opinion just a little bit easier to work with so if I were to go down here and copy it I can actually go in and instead of writing array I can wrap this around a pair of square brackets so I can go in here and add these square brackets and just like this we now also have an array and just to make things a little bit easier for you what you can also do if you wanted to is you can actually add this down to multiple lines just to make it a little bit easier for yourself it’s really a personal preference if you want to go inside your code and have everything separated on a separate line or if you want to have one big line with everything inside of it I do also want to mention one more thing here when it comes to this particular way of writing things because you can also add a comma at the end here or inside the other one up here we can add a comma which is called a trailing comma this is something that we weren’t allowed to do before PHP 8 but now the PHP 8 is out we can actually do this if you at some point expect to add more pieces of data behind it so this is not really something where you have to like make sure there’s no come at the end because oh no then it’s going to break things but we can actually leave in a comma if you want to personally I don’t like to add a comma at the end because that’s how I’ve always done it because that’s how we were supposed to do it but just to tell you that you don’t break anything if you have that last comma so now let’s go and delete the top array up here and just worry about having one array inside our code and talk a bit about how we can access this data here when we create an array we have something called a key which is a way for us to access a certain data inside an array as a default the array indexes are actually going to be a integer which means that we can go in and just use a number in order to access a certain piece of data inside the array so what I can do is I can go below here and say I want to Echo out my array called fruits square brackets semicolon and then inside the square brackets I just simply refer to the index that I want to access what I mean by index is that we go inside the array and the first piece of data is going to have an index as zero so we can actually write that over here just so it makes sense for you the second piece of data is going to have an index as one and the third one is going to have an index S2 I do often experience that people have a hard time wrapping their head around starting at zero when they have to to count something when it comes to programming we always start at zero so the first piece of data is not going to be one but it’s going to be zero so let’s say I want to access banana then I would actually go inside the brackets down here and say I want to Echo out fruits with the index of one so where to do that go inside my browser and as you can see I tried to dim it down a little bit so it wasn’t completely white and burned your eyes out um but if we were to refresh the browser you can see we get banana so in this kind of way we can Echo our data from inside an array using a index number now let’s say I also want to go down and actually add another piece of data to this array here what I can do is I can refer to my array so I can go ahead and grab the name of it and anytime we refer to an array we always need to make sure we add the brackets behind it because this means that this variable is an array and I want to assign something to it so we can set it equal to let’s say orange because that is another type of fruit if I were to do this it is going to take the orange and move it behind the array so now we have apple banana cherry and orange behind it and that of course would have an index as three so if we were to go below here and say instead of echoing out down here we’re going to go below and Echo out a index of three and if we were to do that go inside the browser you can now see we get orange and in the same sense we can also go in and say we want to change a certain part of the array so let’s say instead of banana banana banana we can go in and say that okay so we don’t just want to assign the array to something new but I want to go in and replace the index number one with orange so now we’re actually replacing banana with orange so if we were to go down here and say I want to Echo out the index one save this go inside the browser you can now see we’re going to get orange so now we no longer have banana inside the array at least when it comes to below this line of code here in between here by the way we still have banana as being index number one but as soon as we add in this line here anything below is not going to have banana inside the array but now we do also have a way to delete data from inside the array so if I want to remove a piece of data I can go in and use a buil-in function inside PHP which is something we will talk about in a upcoming lesson because build-in functions is something we use all the time we did also use a bunch of build-in functions in the previous video where we talked about how to create a calculator uh but we haven’t talked about buildin functions yet but we will get to it so what I’ll do is I’ll use a build-in function called unset and I’m going to say I want to grab my fruits array and then I want to say I want to remove the index number one so in this case here we still haven’t replaced banana because we took out that line of code so now we no longer have banana inside the array so if I were to actually try to Echo this out inside the browser using the echo down here you can see that we’re going to get a error message because oh undefined array key one because we have no data inside of it but now you could argue that using unset here is not really going to be deleting an index from inside the array because technically we might want to have the Cherry being moved back on index because right now we have an index as zero and as two and index number one has nothing inside of it so if I just want to completely take out banana instead of just deleting it or unsetting it we can actually go in and use another build-in function called array uncore splice which is another function we have that we can use where we go in and give it a couple of parameters so in this case here I’m going to tell it which array are we talking about and in this case we’re talking about fruits then the second parameter is going to be where do we want to start deleting from so again we count using indexes so if I were to go in and say okay so we start at index number zero so I have to refer to index number zero and then from zero and then one forward I want to delete inside this array so we’re going to delete one length ahead which means we’re going to take out banana and doing this and then going inside the browser refreshing you can now see that index number two is going to be Cherry it is important to point out here that we do also use array splice to insert other arrays or other pieces of data in between certain array elements but I’ll talk about that a little bit later at the end of this video here so now that we know a little bit about arrays let’s go and talk about keys because right now we’ve used numbers which might not make a lot of sense if you want to have a specific label for each index inside the array so what I can do is I can go and delete what we have here and instead let’s go and create a array called tasks as in like house chores or something so what I’ll do is I’ll set this one equal to an array and let’s go and move this down to separate lines cuz that is going to look a little bit easier for the eyes here so in this case here let’s go and say I want to add a name so I want to say that Daniel has a certain house chore inside this house here what I can also do is I can add in a second person so we could also say we have freedom I can also add in a third person Bess and I can also go in and add a fourth person so in this case here let’s say Bella so in this particular array here I have a bunch of house chores and each person has a name inside this array because they have to do a certain house chore but how can I assign a certain key not as a number but as a string to each person so I know which person is doing what inside this house so what I can do is I can go inside and create what is called a associative array which is when you have an array that has a bunch of strings as Keys instead of numbers this means that I can go in here and say that I have a chore which is called laundry and I can assign Daniel to this particular key so by creating a string and then pointing to a value we can do it in this sort of way in order to create a different types of key so we can actually go ahead and just copy this down and I’m going to go and change the name for each one of these so the first one is going to be laundry the second one is going to be trash and we do also have a chore called vacuum so we can say vacuum and the last one is going to be dishes so what I can do now is I can actually go down below and Echo out a piece of data from inside this array but instead of using numbers I can go in and say we have an array called tasks and I want to reference to a certain key and let’s say in this case I want to figure out who’s doing laundry inside this house here so I can go ahead and say I want to refer to the laundry which means it’s going to Echo out who’s doing the laundry inside this array so I can go back inside the browser refresh it and you can see we get Daniel so this order of way we can create a associative array and actually have a label for each of the data inside the array so now we talked about how to Output one piece of data from inside all of these arrays but let’s say I just for developer purpose want to know exactly what is inside the entire array just to see if something went wrong inside the website you know just to kind of like double check what exactly is inside a specific array what I can do instead of echo is I can actually go and use something called printor R parentheses simp colon that I can paste in the name of the array that I want to actually print out inside the browser so in this sort of way I can go and save this go inside my browser and then you can see we get a complete list of all the data inside this array and right now because this is a associative array you can actually see we get the key then we set it equal to a value then we get the next key and then we set it equal to another value but had this been a index array using numbers then of course this would have been zero pointing to Daniel and then one one pointing to feta and so on and let’s talk about a few other built-in array functions that we can use now there is a lot of them we can use but I just want to show you some of the more basic ones now we do also have something called count which means that we can actually figure out how many pieces of data is inside an array so we’re going to use count parenthesis and then we’re going to go and paste in the array so I can go in and paste it in and then you can see when I go inside the browser that it’s going to tell me you have four pieces of data inside this array here one users of this particular function could be if we were to grab data from inside a database because when we grab data from a database it gets returned as an array so we can actually use this function here to count if we actually had any sort of data return from the database so that’s one particular usage of using count inside PHP but we do also have other pieces of functions we could use for example if I wanted to sort an array I can do so in a ascending order which means that if I were to take this task array up here and put it inside the parentheses I am now sorting it ascendingly which means that we’re going to take letters like a first and then it’s going to be b c d e and so on which means that it’s going to go in and sort these alphabetically if any of these values had been numbers then of course one would come first or actually zero would come first and then 1 2 3 and so on so we were to do this and go down and say print R to get this array you can see that we’re going to get the array but it’s going to be in a different order now so if we were to save this go inside the browser refresh it you can now see that b is going to come first and then Bella Daniel and then feta and it’s actually going to give it to me as a indexed array not as a associative array then we do also have one called array push and for that one we do actually need to use a nonassociative array so if would have commented this one out and paste in the previous one we did called fruits I can go in and say I want to take my fruits and I want to use a array push and say I want to grab this fruits array put it inside the array push function which means that we’re now pushing a piece of data at the end of the array which means that I can go in add the array that I want to add some data to and then tell it what kind of data I want to add so in this case here I can say mango and of course a semicolon at the end here and then I can actually go and print it out so we can take the fruits array go inside the print R and go inside the browser and then refresh it and then you can see we have mango pushed in at the end here of course this is very similar to just going in and just like we talked about at the very beginning just going in and grabbing the array so we’re going to say fruits we have an array and then I said equal to a new piece of data so this would actually do the same thing in this case here and the reason that’s relevant is because if I were to do the same thing to a associative array so if I were to comment out my fruits array here and instead let’s say we’re talking about the task array up here which is a associative array which means that we have these different indexes which are actually strings if I want to add another piece of data at the end here I can’t use array push because that only works for indexed arrays so would actually have have to go in and use my last method down here so I’m going to go and say I have my array tasks and then inside the brackets I’m going to go and add in a new string which is going to be the key so I can say we have something like dusting which is going to be assigned to a person called Tera so this is how you would push in a new piece of data at the end of a associative array so if I were to actually go down and print out this array here so we can actually just delete what we have up here and say we want to print out tasks inside the browser you can now see that we’re going to get Terra at the end here for the dusting assignments and the last build-in function I want to mention here is one that we have talked about already so if we were to go in and delete everything except for my fruits array because that’s the one we’re going to use in this example here and let’s go ahead and write down the array undor splice that we already used previously so we’re going to say splice and parentheses and semicolon so like we talked about if I wanted to delete a certain piece of data inside this array and I wanted all the other indexes to move back one slot what I could do is I go in and say okay which array are we talking about we’re talking about the fruits array right so I put that in here and then I say from where do you want to delete from so I could say for example I want to delete from index number one and then I want to delete one ahead of it so right now we would actually take out the cherry and then replace it with something else but let’s say I don’t want to delete something but I just want to insert some data in between this array somewhere what I can do is I can say I don’t want to delete anything by writing zero and and then I’m going to go and add the name of another piece of data at the end here so I can say I want to add in a string and I’m just going to go and add in mango just to have it here so if I were to actually print all this inside the browser you can notice that we now have mango in between banana and Cherry so I paste it in actually I’m mistaken here because I’m not actually going to insert it in between banana and Cherry we’re actually inserting it after Apple so in between apple and banana and the reason for that is that we did actually say to start at index number one which is here so all this data is going to get pushed over to leave space for the new data so that’s what we’re doing here uh so if I want to put it in between banana and Cherry we have to write two because we’re now going in saying 0 1 2 and this is where we want to insert the data which means all this data here has to move over in order to make room for it so if we were to save this go inside the browser and refresh it you can see that we now have apple banana mango and Cherry but this is not just when it comes to one piece of data so what I could do do is I could insert an entire array inside another array so I can actually go ahead and say you know what we have another array up here I’m going to go and call this one test or something just rename it something so I’m going to set it equal to a array and I’m going to go and insert some more pieces of data in here so could actually say we have mango and I do also have strawberry and then instead of saying I want to insert mango inside these when I splice them I can actually refer to this array up here and just say I want want to grab this array I want to go into slot number two I don’t want to delete anything from inside the array but I do want to merge test into that array so if we were to save this go inside the browser you can now see that we have many pieces of indexes inside this array here so in this kind of way we can create arrays and manipulate them and change data and replace data and just do all sorts of things when it comes to arrays because arrays is something you will be using quite a lot inside PHP code for all sorts of purposes so learning how to do arrays is something that is very important to do now there is one last thing we could talk about here which is a little bit more complicated than just talking about arrays which is something called multi-dimensional arrays and essentially this means that we have an array that has arrays inside of it so now we’re going Inception here there’s an array inside an array so let’s say I have an array called fruits and I’m just going to go and move everything down to the next line just so we have it a little bit easier to see and let’s say instead of calling this one fruits I’m going to call this one food and then I want to have different food types inside this array here and instead of Apple we can actually go inside and say we have another another array by using the array keyword and then go in and say we might have something like so I can say we have a apple and we also have a mango and in this sort of way we now replace one of the indexes inside the food array with another array which means that if I want to refer to for example apple or mango I need to go down below and I want want to Echo out the food array and say I want to grab a certain index and in this case here I want to grab the index number zero because that is the first slot inside this array so we’re going to say zero but then I need to tell what the index is of the array inside index number zero so I need to go after add in a second pair of brackets and go in and say I want to grab the first index which is index number zero and Echo that one out which is going to be apple I hope that makes sense okay so we’re we’re going to go inside the browser and as you can see we’re going to get apple because we’re grabbing the uh first array inside this array which has Apple a mango inside of it so if I wanted to grab mango I could also go in and say I want to grab the uh first index which is going to be the second piece of data inside this array here so we to do that we now get mango and if we wanted to grab banana then we just going to say we want to grab the first index and don’t add in the second pair of brackets so we can go in and then get banana but now what about associative arrays cuz we can also do that if we wanted to so let’s go and go back to our Apple examples now it’s just a very basic array uh let’s say I want to add in a food group and I want to say this one is going to be fruits and I want to point to a array which has a bunch of data inside of it so I could say fruits is going to be apple I do also want to add in banana and then I want to add in a cherry and then instead of having more fruit down here below we can also go in and just just copy this paste it in below and say we might want to have meat and then we can have something like chicken we can also have fish and then maybe something like sheep just so we have a little bit of data inside of here or just have two pieces of data you can also do that if you want to you don’t have to have three every single time and let’s go and add in a third group of food so in this case if we could for example say vegetables vbl I think that’s how you say it in English you write it out veg but you say it vexes okay I do know the difference uh so what I can do is I can go in here and I can for example say cucumber and let’s go and add in a carrot so in this sort of way we now have a bunch of arrays inside an array so we have a top category called food and then inside of here I can now refer to a type of food so if I want to grab something within the vegetables I can go and add that in then afterwards add another pair of brackets and say I want to Output index number zero which is going to be cucumber so going inside the browser we can now refresh and then you can see we get cucumber and of course you can also go inside this array here and add this as an associative array so if you don’t want cucumber but you want to have something pointing to cucumber you can also do that so you have a associative array within an associative array um but just to tell you that this is how you would normally go around doing this if you want to have an array inside an array and this is something we call a multi-dimensional array so with that said we now talked about arrays in pretty detail I think for now so with that said I hope you enjoyed this episode and I’ll see you guys in the next [Music] [Music] video so up until now in these many lessons we’ve had we’ve talked a bit about build-in functions inside PSP which are functions that exist inside the PSP language that we can use in order to do many different operations inside our code and there are so many functions out there that we can’t do one episode and talk about all of them since it’s going to be a very long episode if we had to do that and honestly I don’t know all the functions because there’s so many of them but what we can do is we can talk about some of the more popular ones that you might want to use at some point inside your code for various things so in this episode we’re going to talk about some of the functions that are built into phsp that we can use for different things so inside my document here I’m going to scroll down so we have our little PHP section and the first functions I want to talk about are some that are related when it comes to using strings inside your PHP code so let’s go and create a string here so we can actually do something with it so I’m going to create a variable I’m just going to call it string so we have some kind of name for it and then I’m going to say hello world it is important to mention here that whenever we create a function or reference to a function inside our code we do so by referring to the name of the function followed by a pair of parentheses because the parentheses are going to be used to call upon that function so we can do something with it so what I can do here is I can say we want to Echo something out and I can call upon a function called string length which is written by saying St Len and that stands for string length and what we can use this for is to call upon a function in order to tell us how long a certain string is so if we were to actually save this by pasting in the string inside the parenthesis refresh the browse so you can see we get 12 and the reason we get 12 is of course because we have 12 different letters or empty spaces inside our string so if we also count the empty space in there we do have 12 different characters we do also have a function that can go in and actually tell us the position of a certain string inside this string here so what I can do is I can say something like string position and string position is going to require two different things first we want to have the actual string that we want to get the position from so in this case here our variable string then I want to say comma which is a separator to add a second parameter inside this function here and then I can tell it what I want to see the position of inside the string so in this case I could say okay so what is the position of O inside this function here so in this case I’m can go down and say I want to get o save it go inside refresh and then we can see we get four and the reason we get four is because we always start at zero when we count inside programming so 0 1 2 3 4 which is going to be the O so now we got the position of O inside this string and it’s not just when it comes to a single letter we can actually check for more than just one letter so I can go in here and say I want to check for wo so in this case that would be this location right here so I can refresh and then we can see we get six now we do also have a function to replace a string inside another string so what I can do is I can say something like Str strore replace and by doing so I can also go in and say what do I want to replace inside this string here so I’m just going go to delete everything we have except for string so the first parameter is going to be what do we want to replace so in this case it is going to be world and I want to replace it with something else and in this case we can just say Daniel so we say hello Daniel inside this sentence so if we were to save this go back inside the browser you can see we get hello Daniel because I replace world with Daniel then we can also go in and use another function in order to to convert all the different letters to lower case so if I were to go in and say I want to string to lower then I can save it go inside refresh and then we can see everything is lowercase no longer have these uppercase letters inside this sentence everything has been converted to lowercase and the same thing we can do it the other way so we can say string to Upper which would of course do the opposite so were to go in you can see everything is uppercase and if I want to be a little bit more complex here what we can also do is we can do something called substring which is something something that I do occasionally use so if were to go in here so I can say we have a sub string by saying subst and what I can do in here is I can go in and say that I want to grab a certain part of the string here so the first parameter is going to be the actual string so in this case that this is going to be hello world then I’m going to say comma and then I want to say where do I want the offset to be so where do we want to start from and in this case here I could say we want to start from the index of two so if were to go inside my string and count this is going to be zero one two which means to be started here and then I want to have a length set to two so from here and then two forward which means that if I were to save this you can see that we get LL and we can actually do something quite smart here because if you want to do something in a very long string and you want to start from a couple of letters into the string but also end it a couple of letters at the end of the string instead of having this long string and then saying okay so you know 22 characters later I want you to like stop grabing the string instead what I can do is I can say minus 2 which means that it’s not going to start counting the index from the start but from the beginning so in this case here it is 01 2 which means we start at L and then minus 2 is going to be 012 which is the other L over here so as you can see we get low worldl cuz right now that is what we’re grabbing and with this we do also have a another one called explode which sounds very dramatic but essentially what this one does since we now have already talked about arrays in the last episode this is one we can actually talk about uh it takes a string and it says okay so where do you want to take apart this string and then all the different parts you take apart are going to be placed inside an array because we might have multiple pieces of Parts when we explode this string what I want to do is first of all remove the parameters inside of here except for the string and I want to go in and say that the first parameter because this has to be the first one is going to be what do I want to be the divider whenever I explode the string so let’s say I want to take any sort of spaces inside a string and divide the entire string depending on the spaces what I can do is I can say we have a string and that is just going to be a space which means that now it is going to take all the spaces inside the string and cut it like a scissor which means that now we have hello and world two separate strings and it’s going to place these inside an array now in this case here of course we can’t Echo it because that would actually create a error we can actually check this out because this is actually going to be a array so instead what we can do is use another build-in method called print R which is going to be printing out um I believe it stands for print readable if I remember correctly so essentially it’s going to print out data in a readable format so we can actually read it which in this case because of an array uh we might want to read what is inside the array so what I can do is I can just include it inside my print R and refresh it and then we can see we to get hello world so if I were to go in here and just say that we have more than one space so I can just separate the word a couple of times you can see that we get a even longer array because we have more divisions or explosions inside this array here so now having talked a bit about string functions we do also have some related to math inside our PHP code because in a lot of cases we might want to do math at some point uh so having some math functions is something that is really useful uh so let’s go and delete our function down here and also change our string because right now we don’t need to have a string uh but we do need to have some numbers so we can actually perform math inside this little example here so what I’ll do is I’ll create a variable and call it number and then I’ll go in and instead of a string I want to say something like minus 5.5 just so we have an example to use the first one I want to show you is a function called apps which stands for uh absolute I believe you can actually go in here and check it out because there is a really smart function inside our editor where we go in and hover our cursor on top of a function and then it gives us a little description here and as you can see it says absolute value uh so we can get the absolute value of a certain number so if we were to go in here and paste it in so in this case when we have minus 5.5 it is going to get the absolute value which basically means that it’s going to get uh the value no matter if it’s minus or positive it’s just going to get the actual value so if we were to save this go inside you can see you get 5.5 especially inside my gamep courses on the channel we use apps a lot to get you know the absolute value instead of having to deal with negatives and positives and stuff like that so it’s just a really neat function to have we can also round this up so we can use a function called round and this is basically going to do what you think it’s going to do it is going to round this up to the nearest uh whole number so when I refresh it you can see we get minus 6 then we also have something called p w which is going to be the power of or exponential expression so in this case here we can’t really use number because that is not really how this works but what I can do is I can feed it to number so I can say two comma 3 so again this would be exponential Expressions which means that we have to say 2 * 2 which is 4 and then 4 * 2 which is 8 in this case here so if we were to save this refresh it you can see we get eight then we can also do the square root of something so we can say sqr t which means that we’re going to get the square root of one certain number so let’s say two in this case and then you can see we get 1.4 it might have been a little bit easier if we did something like 16 just to like show this in a little bit more normal way so 16 the square root of that is going to be four but now another one that I have as a favorite is one called random so we can actually go and say R A and D which is going to give us a random number between two numbers so I can go in here and say I want a random number between one and 100 and in this case here we’re going to go in when I refresh it oh okay so in this case it’s going to be 51 if I refresh again oh it’s going to be 49 now it’s going to be three now it’s going to be 6 now it’s going to be 10 so it’s just going to keep feeding us a random number each time I refresh the browser I’ve seen people use this one especially when it comes to reloading images inside the website as they’re developing a website because your browser do have something called a cache that is going to store certain data so it’s easier and faster to load your website the next time you refresh it so when you’re sitting there develop and constantly maybe swapping out a certain image and it’s not really changing when you refresh the browser because oh it stored the cash of your last image so it’s not showing the new version then you can use this one and put behind the image name in order to get a new image but again that’s more of a developer trick you can use but it’s not really something you should use for releasing a website since there’s a a reason why a browser stores a cash of your images that’s because it’s easier to load next time right so now this was a couple different math functions we could use but what about arrays cuz we learned about arrays in the previous episode uh we do have quite a few different array uh functions I did cover some of them in the last episode but we do also have many others I could show you so let’s just go and create a array called array and inside of here we can fill it in with fruits just like we did in the last episode so we can say apple we can also say we have something like banana and we do also have orange and then we can just manipulate this array using many different functions the first one is going to be one called count which is actually one that we use quite often when we pull data from a database I do think I showed that one in the previous episode but let’s just go and talk about it here so if I were to say Echo and say I want to Echo out a count function that has the array inside of it we can actually see how many pieces of data are inside this array here so we do that you can see we get three because we have three pieces of data so let’s say we have an example where I pull data from a database and I need to check did we actually get anything from the database because if I didn’t then there’s no need to run a bunch of PHP code because we didn’t get anything from the database so this is going to be a very useful one that you will get to do a little bit of when we actually start talking about databases inside my episodes here uh so this one is worth remembering but now we do also have one here where we can go in and say is something an actual array so we can say is array so isore array and this one is going to give us a true or false statement so in this case it’s going to be false so it’s going to be true and like we talked about when it came to our data type episode a zero means false and a one means true so in this case it did actually return as true because this is in fact an array and we did also talk about a few other ones in the last episode that we can just briefly uh just mention in this one so if I were to go in here and say we have something called array undor push we can now push in data inside our array at the end of the array so I can go in and say we have this array here and I want to add a piece of data which is going to be a a kywi just to add something else to it so if I were to actually print R this one because in this case it would do actually need to print R in order to see all the data and of course I want to have this after we array push so I’m just going to move it down to the next line here save it go inside and then you can see we get kyv inside our array at the end there but now one we didn’t talk about in the last episode is how to remove data from inside the array so what I can also do is I can go below here and say that I want to do something called array pop so array po o and inside this one we just need to have one parameter since we’re only removing the last array index if we were to save this one and actually Echo it out or print R it out inside the browser then you can see we get the same array but now kyv has actually been removed at the end here so we no longer have it again and we can also go in and use another function where we actually reverse the entire array so if we were to go in here and say we have this array here but this time I want to array uncore reverse the array so parenthesis and then I’m going to insert the array inside the parentheses here then you can see we get everything but it’s reversed so we get orange first banana and then Apple and now in the last episode we did talk about something called array undor splice which means that we’re splicing two arrays together and merging them into each other uh but we also do have something called array uncore merge but merge is a little bit different because it’s going to take the data and put it at the end of the first array whereas splice is going to allow for you to put it anywhere in between any of the the existing data inside the first array so if we were to go in here we can also do array uncore merge so we’re going to say merge and then I want to have another array up here so I can just copy paste this down call this one array one this one array two and let’s go and change some of the data inside of the second array so again we could just use kyv and say we want to merge these two together so we were to do this I can say that I want to have the first array which is array one and the second array which is going to be array two too and when we do this I can go back in print it out inside the browser and then you can see we get kyv which has been placed at the end of the other array so all the data inside the second array here is just going to get added to the first array up there because we merg them together at the end of the first array so just a few more array functions you could know about after we talked about in the last episode but we do also have something else because inside phsp in some cases we do also deal with dates and time and when it comes to these we do need to know how to get the date and time because for example again if we talk about databases in a lot of cases you might want to know exactly what the time is when you upload certain data to a database so you might want to have a datetime format that you need to use in order to get the date time so let’s go and start by echoing out a certain date so what I can say is we have a date function and inside this date function I want to tell it the format that I want to Echo out the date and I will include a link in the description for all these different formats if you want to see them uh but essentially if you want to get the full year you can go inside and use a capitalized y then you can separate it with a dash and then we want to get the month the day and then we can also get the time which is going to be the hours colon then we want to get the minutes and also the seconds so in this case if we were to go inside the browser you can see we get the uh full date with the year month day and the current time inside our little uh function here but now we do also have something else we can use so if we were to go down here and say I want to Echo out the time so we’re going to say time parenthesis then you can see we’re going to get this weird long number inside the browser here so you can see we get this random string and you might be thinking hm so what does that do well let’s try and refresh the browser again and see what happens oh it updated okay so if I click this every second you can see it’s updating by one number every second that I click because this is the seconds since a certain date in history I can’t remember the exact date but what we can do is we can go and hover our Mouse CR on top of the function oh and it actually provides a link to the pspnet website so I’m going and open this one and then you can see we get this little link here what explains that it is since January 1st 1970 that we do get the amount of seconds since then and the reason this is something we can use is because you might want to get a certain time difference between now and now so we can actually subtract these two different numbers and get the amount of seconds and this by the way is called a Unix Tim stamp which we can also use for the next function where we actually go in and we want to know what is the Unix timestamp for a certain date at some point so what I can do is I can actually create a string so we can just go and call this one variable date and then I can set it equal to a random date at some point again using the same format that we talked about before for which was uh the date time format so I can go in paste in a random date from my notes here and then I can go in and say that I want to get the string to time Str str2 time and then I can paste in the date and see what is the Unix timestamp for this particular date here and in this case here it’s not going to update every single time I click it because we’re getting a fixed date or fixed time of seconds from 1970 the 1st of January until this particular date that I have pasted in here which is 2023 uh the 11th of April which is today and we have many other different types of date functions you could use inside your PHP code and the same thing goes for the strings and math and we also have something when it comes to like files inside your uh your PHP or files inside your website you can delete files or do something else to it using functions and I will leave a link to many different functions you could be using that you might want to check out and I will have that inside the description of this video here so if you want to check out something on your own then you’re more than welcome to do so but for now these are some of the different functions you can use in order to change things inside your phsp code so with that said I hope you enjoyed this little episode on built-in functions in next one we’re going to talk a bit about userdefined functions which is a lot more fun because now we actually get to create our own functions which means that we can start creating a function called something specific followed by a pair of parentheses and then we can decide that we want to add parameters to this function to change how the functions work and stuff like that and functions is something that we use a lot inside our code pretty much constantly anytime you create anything using phsp uh it’s going to have functions inside of it so functions it’s important and that’s what we’re going to learn the next episode so with that said I hope you enjoyed this one and I’ll see you guys next time [Music] today we’re going to learn about something called userdefined functions inside PHP and this is the moment where we start moving into a little bit more of an interesting point inside our PHP lessons because userdefined functions is where we can start creating our own functions inside PHP in case there is some kind of function we need that isn’t built into the PHP language user defined functions is something use constantly inside our code and I do mean constantly because we use functions in order to not have to rewrite code as well over and over again because if I need a certain piece of code to do something and then later inside my application I need to do the same thing instead of having to recreate the same code in two different places we can use this userdefined function just to reference to it so we run the same code but the code is just sitting in one place somewhere I do also want to point out here because I do remember back when I was learning PHP like 12 years ago when I took my bachelor’s degree in web development that the teacher would ask me inside the exam so what do we use a userdefined function for and I would say well we use it to do things inside our code but the answer my teacher was looking for was okay so we need to use a user defined function for one particular thing so if you want to create a bunch of code your function should not have a ton of code inside of it doing a lot of different things it should have one particular purpose in mind so don’t use a function to store like an entire script inside of it it’s just meant to do one particular thing we’ll get to do a little bit more examples so you can see what I mean so you know not creating a lot of code um so what I want to do here to begin with is let’s go ahead and just create a function just to see how do we create a function and how do we name it and how do we add parameters to it and and write code inside of it uh so what I’ll do inside my little PHP script here is I’ll go in and I’ll use the function keyword which means that now we’re creating a function and I want to go in and give it some kind of name and you can name this function function whatever you like we can come up with any kind of name that we think is appropriate for this function but there’s of course some naming conventions just like with variables and that kind of thing so the way we usually do it when we start a function is we start with a non- capitalized letter for the name so if let’s say I want to say say hello then you can do something like this where every word after the first word is going to start with a capitalized letter or if you want to do it you can also go in and say you want to use a underscore instead now my personal prefer is to do a capitalized letter because that’s how I see most people do it but it’s really up to you like there’s different naming conventions you can use here after creating the name we need to actually tell it that this is a function by adding the parentheses and these are the same parentheses you see when we create a builtin function so if we were to go up here and say something like string length then you can see that we can go in and actually get the length of a certain string so if it were to say Daniel and Echo this out inside the browser we would get the number of letters which would P six in this case here inside the browser and this is what we need to use the parentheses for because we can decide how many parameters we want to pass into our particular function down here because this is our function we can do whatever we want with this function here so I can decide how much do I want to pass into my function now you can also just decide not to pass anything inside your function so let’s go ahead and go inside the curly brackets here which is going to be where all the code is going to get run that you want to run inside your function so I’m going to go down here and say say I want to Echo a string and I just want to Echo out hello world so by creating this we now have a function inside our code that we can call upon so we’re not actually running this code yet we only run it once we call upon it so if we were to go inside my browser just to prove my point if I were to refresh you can see there’s nothing inside the browser but if I were to go down here and actually run my code by running the function I can go in and say we have a function called say hello hellow parenthesis semicolon and then I can simply run this function by Saving refreshing the browser and then you can see we get hello world and now we did use Echo inside the function here which is something you can do but it is a very typical thing inside a function to not Echo out a value but instead return a value uh so what I can do is I can go in and say return and when I refresh the browser you can see we don’t actually get anything inside the browser but instead I would have to go down and actually Echo our function so if we were to go in now you can see we get hello world and one of the reasons for this is I can actually go in and create a variable and I’m just going to call it test and I’m going to set it equal to my function and because I’m returning a value and not echoing a value I am now actually going in and assigning hello world to my variable test because this is the data that my function returns so now if I were to go below here and say I want to Echo out variable test then you can see inside my browser we get hello world but now we did talk about passing in parameters because we can do that and we can decide if we want to do it because it’s our function The World Is Ours we can do what we like uh so what I can do is I can go in and say okay let’s go ahead and pass in a piece of data now I’m just going to use a placeholder for this data so I’m going to create a variable and I’m just going to give it some kind of name so this could be name for example which now mean that when you call upon this function down here actually see we get a error message it now demand that you pass in one parameter in order for this function to actually work so if we were to go down here you can see that if I were to go in and say okay I’m just going to pass in a string and this is going to be Daniel we’re now passing in a piece of data that is going to be assigned to this variable placeholder up here so we can now use it inside our function so if I were to go down here and say that I want to concatenate so I’m just going to go ahead and say we want to concatenate our variable here just going to grab variable name which is the placeholder or the reference that we need to use inside this function save this one you can now see that if I were to go inside an echo the function down here you can see we get hello Daniel and you can pass in as many parameters as you want so you can go in here and say you want to add another name so this could be last name uh we can also pass in variable pet so we can you know get the pet that we might have inside our household or something uh so we can pass in many different parameters and you can also see they don’t actually light up they’re kind of like Gray out inside our editor which means we’re not actually using this parameter inside our function and just to mention it here because you don’t actually have to use these variables inside your uh little function down here but you do need to pass in the actual parameters inside when you call upon the function I do have two more things I want to show with this example here so let’s go and go back and just have variable name inside our function so what we can do is we can also assign a placeholder and by placeholder I do mean a default value so let say I go down here and I decide not to pass anything inside my my function down here and it’s now giving me a error message H is there a way for us to run this function without having to pass in a parameter and it just assigns a default value if you decide not to yes there are so what we can do is we can go in and inside the parameter inside our function up here I can go and assign my variable name to Daniel actually let’s go and call it something else so we get something else inside the browser here so let’s say Bess so now if I decide to call upon this function down here without actually passing in a parameter you can now see that it’s going to assign Bessa as a default value for this particular parameter here however if I go down inside my function and say you know what let’s let’s have it be Daniel and I save it refresh you can now see that oh okay so there is a value passed into this function here so we’re not going to use the default value that I assigned inside this function here the second thing that I want to show you is also in terms of something called type declaration which is also something we received in PHP 7 something I do believe type declaration is something we have in most other programming languages but for some reason because PHP is a very Loosely typed language we don’t have it and you don’t have to have it inside PHP but what you can do is you can go inside your parameter up here and say okay I do demand that whenever a user or whenever a programmer call upon this function here it has to be a string that is passed into this function here so what I can do is I can say I want to have this being a string data type so whenever we pass anything in here it has to be a string so right now if I were to save this it is not going to give me any sort of error messages because it is a string but if I were to go down here and say I’m passing in a number instead I’m now going to get a error message actually it’s not going to give us any sort of error message because right now we don’t actually have strict types enabled inside our code so that is something we need to do first uh so if you want to have these type declarations you do need to make sure that the very first thing inside your script here is going to be a strict type declaration so we’re going to go to the top of our file I’m going to open up my PHP tags and I’m just going to go ahead and close it again and the first thing inside my PHP code is going to be a declare strict types equal to one which means it’s going to be true so if we were to save this and then go down you can actually see oh now we do actually get a error message because o this is supposed to be a string but we found a int number and if we were to refresh inside the browser you can see oh okay so type error which means that we don’t insert the correct type inside our function here so type declaration is something that can help you make sure that the right data is being inserted inside the right functions so this is something I do recommend using because this is something that I’m used to from other programming languages but again if you want to you don’t have to do any sort of type declaration inside your Cod you can just delete this liner code here and delete the type decoration like you don’t have to have it it’s very very much possible to do without but I do recommend doing it because it is a habit I have from previous code that I have written in other programming languages out there and there is a reason for why we have it it is to make sure that we have one less error that we might accidentally do inside our code so it’s just kind of like a neat little thing we got added to PHP so with my strict type decoration I can now go in and pass in a string and then you can see oh now it works so and again we can use functions for many different types of things we can also go inside and say this is going to be named calculator because we did create a calculator in our exercise in the last exercise so what I can do is I can go in and say I want to pass in a integer and I want to call this One n one and then I can say I want to pass in a second integer and this is going to be number two and again I’m just going to declare these strict type declarations here because I do think it’s a good idea then I can go inside my code down here and say I want to run a calculation so variable result is going to be equal to variable num one plus variable num two again this is a very simple calculator here it it only knows how to add apparently but I’m just trying to prove a point here so what I can do is I can return a value so I’m going to return variable result so now when I go in I can actually call upon this function here and I do need to pass in the right data so I can go in and say that I want to pass in a number so this going to be two and five so we were to do this refresh then you can see we get seven I do also want to mention here that we do have something called scope inside programming in all programming languages out there and a scope is essentially where you can access certain variables so right now inside this function here this variable called variable result is a local scoped variable which means that we can only access this variable from within this function here one thing that I remember confused me when I started learning programming like many years ago is whenever I went up here outside the function and I would create a variable so let’s say I just create a variable called test and I’m going to set it equal to Daniel now if I were to go inside this function down here can I take variable test go down and just let’s say use it inside this function here so if I were to say I want to return variable test oh we get a error message okay so we can’t do this because it is a undefined variable because it doesn’t exist inside the scope of this function here but what I can do inside this function here is I can actually go and take my variable test and say that I want to grab a global variable within the global scope of my code so I can go in anywhere inside the code here and say I want to grab a global variable or at least declare that this is a global variable that I want to have access to and I can say I want to grab by variable test and because I reference to a global variable in this sort of way we can now use it inside our return down here so I can actually go in refresh and then we can see we get Daniel again I feel like I’m piling information on top of people here but it is just important to know that we have something called scope and we will have a episode I think we should do that in the next episode talk a bit about uh Scopes inside our PHP code because that will be a little bit more relevant to talking about global Scopes and local Scopes and that kind of thing so for now don’t worry too much about Scopes let’s just go ahead and worry about creating functions inside our code like so so you know how to create a function that has a particular piece of code that has a particular purpose in mind uh which you can then use inside your PHP application I do know this is a lot of information I piled on top of people in this video here but it is important to know what a function is because you will be using it constantly inside your code so with that said this is how you create a function if you’re still a bit confused about it I do recommend rewatching the video uh because you do need to know how to create a function okay so with that said in the next video we’re going to talk a bit about Scopes inside PHP and and with that said I about to say it again I hope you enjoyed the video and I will see you guys next [Music] [Music] time today we’re going to learn a little bit about Scopes inside PHP since we talked a bit about userdefined functions in the previous episode and talking about Scopes is going to help you understand how we can access different types of variables and functions and classes and so on so we’re going to talk a bit about Scopes today Scopes Scopes I’m pretty sure I’m saying it too fast Scopes so essentially what a scope is is how or when you can access a certain variable or function or something inside your script this means that right now if I were to have this page that has nothing inside of it and I were to create a variable so I can go in here and I can say I have a variable I’m going to call this one something like test then I need to know exactly when I can access this variable inside my code and we do have four different types of Scopes inside PHP we have something called a global scope which is where my variable right now actually is which means that we can access this variable from anywhere inside of our scripts we do also have something called a local scope which is when we’re talking about inside a function so whenever you define find a variable inside a function you can’t actually use that variable outside the function because it’s locally accessible from within that particular function we do also have something called a class scope and classes is something that we are not really getting into right now classes is kind of something you learn about after you learn the basics of phsp since objectoriented phsp programming is something that you should learn uh but here at the beginning it’s not really something you need to worry too much about but I will give a short example in this video here just to kind of give you an idea about what exactly a class scope is since we’re talking about Scopes so we might as well talk about all of them uh we do also have something called a static scope which is essentially when we have something that is statically declared inside our code uh we need to talk a bit about what exactly that is and how and when you can actually access a statically uh declared variable inside your your code uh so that’s something we’re going to talk about as well so now let’s start by talking about a global scope so as you can see right now we have this script um which we I have introduced The Script already uh but we do have this variable that I just created called test which is said equal to Daniel and if I want to gain access to this variable I can just simply go down and I can say Echo and then Echo out variable test and if I were to do that go inside my browser you can see that we’re actually going to get a spit out inside my browser because this is a global variable and I can just sort of access it from anywhere inside my script so as soon as we declare something outside a function outside a class it is inside the global scope of our script so we can can access it but now we do also have a local scope which we talked about in the previous video where we talked about userdefined function since whenever we create a function inside PHP we do have some code inside the function and we need to know exactly when can we use that code and and how can we use it inside our our script in here so now if I were to paste in a small example that I have here of a function you can see that this particular function here is called my function and we right now inside the function have a variable called local variable and it said equal to hello world now this particular variable I can gain access to within this particular function here so if I were to actually go in and and Echo it out or return it we can also do that because that is probably the better thing to do if we were to go inside and actually return this value go down below and call upon my function here you will see that we actually spit out our return inside our browser so if we were to do this refresh you can see we get hello world but now what if I want to G access to the variable inside the function so if I were to go in and say I want to copy this variable and I want to Echo it out down here because I mean I did declare a variable right but it’s inside the function so we were to do that refresh the browser you can see oh undefined variable which means that there is no variable existing that we can gain access to called local VAR so as soon as we declare a variable inside a function it is locally scoped within that function so we can only access it within my function and the same goes kind of the other way around because if I were to go above this function here and create a new variable so I’m going to say we have a variable I’m going to call this one test again and I’m just going to set it equal to a string So Daniel and if I were to try and access this variable within this function if I were to go in here and say we want to return variable test and actually spit out the function down here then you can see that we’re going to get another error meth because again undefined variable variable test because it is inside the global scope but not within the local scope of this particular function but Daniel didn’t you say that the global scope can be accessed from anywhere inside the script yes as long as it’s not inside a particular scope of for example a function or inside a class then we can gain access to it from within anywhere else inside our script so anywhere else below here if I were to go down here write some code that is not inside a function then I can of course gain access to variable test but as soon as I start creating a function like I did here it is going to only take into account the local scope of this particular function unless I pass in the data into the function and we talked about that in the last video too we can do that by simply copying our variable and say that I have a parameter inside my function here so I can pass in a parameter and then I can go down and for example spit out variable test down here because now I passed it into the function and also we do need to make sure we go down and actually pass it inside the function itself when we actually call upon the function because we have to pass in some data so we were to do this go inside refresh the browser you can now see that we’re passing in data and we can now see it inside the browser but now I did kind of lie to you just now because I did say that you could not gain access to a global variable inside a local function but it is actually possible for us to do so it’s just not really a habit that you should get into doing unless you have a very good reason to uh because the reason we passing data into to the parameter of a function just like we did here is because we want the function to work depending on outside data so if I go in and say well if we have this Global variable then all of a sudden the function becomes kind of like not as reusable in the wrong setting so let’s say we don’t have this Global variable and I say I want to grab it then all of a sudden we start getting some weird funkiness going on so what I can do if I wanted to is I could actually go in and just say we’re not going to pass in this variable which of course is going to give us an nrow message just like before because right now we cannot find variable test but what I can do is I can actually go inside a function and say I want to declare a global variable which right now is called variable test and now because I declared that I want to grab one of the global variables inside the global scope I can now use it inside my function so want to save this go inside you can now see that we get Daniel but we do also have a second way we could do this if you don’t want to declare a global variable first and just directly gain access to it so there’s another shortcut to do this which is also you can just go down and say we want to use one of the super globals we talked about in the previous episode so I can go in and say I want to grab a super Global called globals and I want to pass in variable tests inside my Global’s super Global so in this sort of way we’re grabbing a global scope variable called test using this super Global here called globals there was a lot of tongue twisters in a row so if we were to do something like this go inside the browser you can see we get the same result so it’s basically just another way of doing the same thing as declaring a global variable at the beginning of the function but like I said don’t do this unless you have a very specific reason to do so because passing in parameters is really the way you need to do it in 99% of cases whenever you do variables like this or or functions so now you know how to gain access to a global variable but just know to use it sparingly and for a good reason so now that we talked about a local scope of a function let’s talk about something called athetic scope and athetic scope is something you get whenever you create a static variable inside a function for example you do also have it inside classes but let’s go ahead and take a function as an example here so if we were to go ahead and paste in my little example code here you can see that inside my function we now have a variable that has been declared as a static variable and all I’m doing let’s actually go and just do this to begin with here all I’m doing is I’m going in and saying okay so static variable is equal to zero and then I want to add one to it and then I want to to Echo out the result or in this case here just return the result and then Echo it out down here inside my function so if I were to do this what you should get is one because when I run this function with taking zero and adding one to it and then returning it so we would to go inside my brows so you can see we get one but now what happens if I do this a second time if I were to go down here and say I want to Echo this out again but what is the second Echo going to be is it going to be one or is it going to be two because remember we’re adding one inside function so if we to go inside my browser refresh it you can see we get another one and the reason for this is that a function is meant to be a blocker code that has code inside of it that you can just gain access to over and over again inside your code so you can reuse the same code again and again and again and because of this they should not be influenced by each other so if I use my function in one place then it shouldn’t change the same function when I use it in another location otherwise it kind of defeats the purpose of my function so whenever I use the same function again and again we should get the same result every single time however let’s say I go inside my function here and I declare a static variable so if I use the static keyword instead this particular variable is going to be shared by all the functions inside my code or inside my script anytime I use this particular function here so if I were to go in here and say that my static variable is equal to zero and I add one to it it means that now for all all the other functions in the future this particular variable is not going to reset itself it is going to stay as one so when I use the function the next time the next time the number is going to get spit out it is going to become two and again if I were to use this function a third time it is going to be three because it keeps adding one to it and because this is a static variable it is going to be shared among all these different functions down here now the last scope that we need to talk about is something called a class scope and this may be a little bit premature because we haven’t actually talked about classes and properties and methods yet and objects and and all these things but it is something that exists when it comes to scope so I’m just going to mention it so you know a little bit about what I’m talking about here so inside PHP we have something called a class which is basically like saying we have a template that has a bunch of variables and functions inside of it I’m doing this because they’re not actually called variables and functions uh but so you know exactly what I’m talking about they kind of work the same way as variables and functions but within a class so as you can see here I have a class called my class and inside the class I have a variable and I also have a function which is actually called a property and a method and these variables and functions are only accessible from within this particular class here now I’m just going to stop myself here because I started talking about how to create Optics and different things off this class and this is not supposed to be a class episode It’s supposed to be about Scopes um so all you need to take from this is that when we have a class like this all the different properties and methods within this class let’s actually go and delete this static keyword here because that is actually useless right now um if I were to do this all these properties and methods can only be accessed directly inside this class here so if I were to go outside this class I can’t access these unless I make them static and the reason you will make them static is for like entire different reasons like there is actually a reason you might want to do that um but just for like regular usage you don’t usually do that so this is what we call a class scope so now we talked about global Scopes we talked about local Scopes and we talked about static Scopes and we talked about class Scopes uh don’t worry we will get to talk more about classes at some point in the future but for now let’s just go ahead and stick to procedural PHP and then once we have learned all the basics then we’ll get into like classes and off Tech oriented PHP and stuff like that so for for now that is basically what scope is when it comes to PHP so hope you enjoyed this lesson and I’ll see you guys next [Music] time today I’m going to teach you about something called a constant inside PHP and a constant is basically a way for us to create data that cannot be changed at any point inside our code which which is very useful for a lot of reasons for example if you have some data that is very important to a lot of your code and it has to stay the same then declaring it as a constant makes sure that if you were to accidentally change it at some point you get an error message instead of all your code that starts breaking and and showing the wrong thing because that one piece of data has now been changed and everything else is now wrong so creating constants is something that can help us create data that should always always stay the same if I were to create a variable for example here so if I were to go and say we have a variable called name and setad it equal to a string called Daniel you can now see that if I were to Echo this out and say I want to Echo out variable name this is going to give us Daniel because that’s what we wrote but what I can also do is I can take my variable name go down below and I can change it to something like Bassa and if I do something like this it is just simply going to change the variable in to B now which is okay because this is just a variable however if I were to do the same thing using a constant this is actually going to throw me an error message so if we were to go up here we can declare a constant by saying we want to Define parentheses semicolon go inside the parentheses and give it a name so to begin with here we can call it something like Pi because Pi should never change because it is always going to be 3.14 so let’s say I’m doing a bunch of calcul ations and I need to use Pi in order to do this and I wrote it as 3.14 if Pi were to change then all my calculations are going to go wrong so I want to make sure that we always get this particular value even if I were to accidentally change it at some point in the future which I should not be able to do but hey mistakes happen but we just want to do as much as we can to avoid those kind of mistakes so what I can do here is I can go down and say I want to Echo out not variable name but just Pi if would to saved this go inside the browser you can see we get 3.14 and you may have noticed something here because I did actually create a variable name using all caps so right now it is pi using capitalized letters this is not something you have to do I could also go in here and say Pi with a non- capitalized lettering so if we were to do that and actually spit it out inside the browser you can see we still get 3.14 however it is a convention inside any sort of programming language that when you create a constant that you use capitalized lettering because it shows other programmers that this is a constant so it’s just kind of a visual indicator for other programs to know that this is a constant so now if we were to spit this out inside the browser but I were to go down and say I want to change Pi into 4.14 so let’s say I want to do this but hey whoops I made an accident I accidentally changed something that I shouldn’t change if I were to do this you can now see that we get a error message because we’re not supposed to redefine a variable called Pi elsewhere inside our code and this is just a very useful way to make sure that if I were to accidentally change something that we get a error message instead of all the code starting you know to go wrong and of course this can be any sort of data type so it doesn’t just have to be a float number like it is right here I could also go in and say this is going to be name and then I can create a string and say this is Daniel and if we were to actually spit that out you can see we get Daniel inside the browser we can of course also create all the other types of data we can create a true or false statement so I could say isore admin you know is the person and administrator then I can go in and say this is a true and of course I can go in and actually spit it out so we can see it it is also important to note here that a constant is inside the global scope whenever you define it which means that if I were to create a function down here so if I say have a function just going to go and call it something like test just to give it some kind of name if I were to go inside the function I can actually Echo this particular Conant directly inside the browser because we can just gain access to it so even though we in the last episode talked about variables not being able to get passed into functions unless we actually declared that they were a global variable we should access or pass them in through the parameters or something like that uh we can actually do that using constants so if we we to actually go in here and say I want to Echo up Pi then I can actually do that so if we to go below here and actually say I want to run my function here called test and then you can see that we get 3.14 inside the browser of course we’re going to get 13 now because we also echoed out is atmin over here so we delete that you can now see we get 3.14 the last thing I want to mention here is whenever you define a constant is to make sure to always do it at the top of your script it’s just kind of a habit that we have that whenever we want to create a con they should be all listed at the top of the code so even though it’s not necessary I could take the constants and actually move them below all my code if I wanted to do that it is kind of a habit or a good practice for programmers to make sure that it’s all at the top of the code so we can easily find them and this is basically what a constant is so having talked about that we now start reaching a point where we need to start talking about how to actually do things with databases when it comes to PHP because PHP is is kind of like one side of the coin and the other side of the coin is handling data from within a database so we need to start talking about how to you know actually go inside a database and create data and how can we then grab the data using PHP and show it inside a website so having talked about constants I hope you enjoyed this lesson and I’ll see you guys in the next video [Music] today I’m going to teach you about something called a loop inside PHP which is actually going to be our last episode before we start talking about databases together with PHP so that is going to be very exciting cuz it’s kind of like the point where we can start building some very cool things inside phsp so with that said let’s talk about the different types of Loops that we have inside PSP now if you have learned something like JavaScript beforehand this is going to be very familiar to you because a loop is pretty much the same thing in most programming languages so there’s there’s like slight differences maybe but they are going to be pretty much the same thing so when it comes to a loop we use them inside our code to spin out a blocker code multiple times by just writing one block of code so we can spit it out maybe like 10 times or you know maybe we pull some data out from a database and we want to make sure that we spit all the data out inside our website rather than just one piece of data so we can keep spitting out data depending on how many iterations would tell it to actually run inside our code basically just something that repeats again and again and again and again until we tell it to stop that’s basically what a loop is so when it comes to a loop we have the first one which is called a for Loop and a for Loop is a very basic way for us to spit something out depending on numbers so what I can do is I can actually go inside my for Loop here and inside the parameters we need to include three different parameters the first one is going to be a starting point so I’m going to create a variable we can call this one whatever we want but it is kind of like a tradition to call it I for iteration because one iteration is one Loop and then we can do like three iterations which is three Loops so you know we have something called a iteration to begin with here we’re just going to go and set I equal to zero and then I’m going to end this off with a semicolon and add in the second parameter or the second state inside this uh parameter here and that is going to be when do we want this Loop to stop running so at some point this has to stop otherwise we create something very bad which is something called a infinite Loop which is a loop that is going to run endlessly inside your website and eventually crash your browser so we don’t want that to happen that is a very bad thing um so we want to make sure the loop has a stopping point so what I can do is I can goad and say that I want variable I to stop once it hits a certain point and this can be you know when variable I is equal to a certain number which is for example 10 we can also do less than an equal or greater than an equal let me just go ahead and rephrase that we’re running this loop as long as this statement is true that’s what we’re doing okay so we have to go in and say as long as uh variable I is lesser than or equal to 10 then we want this uh four Loop to keep looping that’s that’s what we’re saying here so what I can do is I can go inside and add a third parameter which is going to be how much do we want variable I to increase or decrease every single Loop when we Loop this out and what I can do here is I can go ahead and use a increment or a decrement which is something we talked about in our operations episode uh basically we can go in and say we have a variable for example variable I and I want to add one to it by writing plus plus so the first time we Loop this variable I is going to be equal to zero but once we get done with this blocker code it is going to change it to one and then the next time it’s going to be two and then it’s going to be three and four until we get to a point where this statement here is no longer true and then it’s just going to stop running so what we can do is we can go inside here and we can just go ahead and Echo out something so we can say Echo um this is iteration number and then we can go and add a concatination and say this is iteration uh variable I and let’s also go Ahad and add a break to this because because we do want to have multiple lines just so we can see it properly inside the browser here so what I’ll do is I’ll just add a HTML break and with this I can go inside my browser and as you can see we get this is iteration number zero and number one number two and so forth until we get to iteration number 10 now in this case here we are spitting out 11 numbers because we’re also spitting out the first one which is you know when the number is zero and there’s a couple of ways you can do this if you want this for example to Loop out 10 times then we could go ahead and say this should be uh maybe equal to you know 9 or we can also go ahead and say that we should start at one uh if I do this for example if I were to go inside the browser you can see that we start at one and then we count to 10 uh we can also go ahead and set this back to zero and say it’s just going to be lesser than 10 and this case it’s going to go and stop at nine so there’s many different ways you can split this out 10 times uh this is basically just to show you that you can go inside your parameters if I can find my mouse there we go you can go inside your parameter here and you can change these numbers however many ways you want to so this is a basic way to create a for Loop inside your code and again you can write whatever code you want to in between these uh curly brackets here so you can you know spit out any sort of code that you want and there’s many different uses for a for Loop for example if you have a string that has a certain number of characters inside of it and then you want to spit out uh a loop for each character so you can count how many characters inside a string and then you can do that based on that basically anything you can think of that has something to do with numbers is something you can use in this case here you could also go in and actually replace this you could say instead of being lesser than 10 uh if I were to have another variable up here I can just call this one test I can set this one equal to five and I can actually go ahead and replace variable test with my number 10 and in this case this would also work so in this case we’re spinning out five times so you can replace these in here with variables if you wanted to you know if that’s the thing that you want to do it’s just kind of to show you that there’s kind of like a a free Ro uh to do whatever you want with this kind of loop here but now we do also have a second type of loop which is called a while loop so what I can do is I can just comment this out just so I can demonstrate what a while loop is and the way this works is instead of using numbers I can actually go inside and create any sort of condition that you might want to use from for example Le a if statement so you know when we use a if statement you can also go and compare things you know do something specific uh we can also create a Boolean so if I create a variable here I just call it Boolean I’m going to set this one equal to true what I can do is I can go inside and say as long as this variable here is equal to true then I want to Loop something out and now of course in this example here we are creating what is called a infinite Loop just like we did previously so it is a very good idea that this should at some point make our variable Boolean into fals otherwise this is not going to be very good for our website cuz it’s going to crash everything uh so what you could do is you go in here and say let’s just go and make variable Boolean equal to false in this sort of sense here uh so this basically means that the first time it’s actually going to just Loop out one time and then it’s going to stop looping again because the first loop it’s going to change our variable into false uh which means that this is not going to Echo out anything else so in this case we’re just going to Echo out our Boolean just so we have something to actually spit out inside the website so we can test that this is working uh so if I were to do this go inside my website you can see we get true or one in this case which is the the number version of being true zero would have been false in this case here so we are spinning something out uh what we can also do is we can actually create something very similar to what we had up here so I can take my variable test and I can go down and say I want to say we have a variable called test and as long as variable test is lesser than 10 then I want to spit out this Loop here so what I can do is I can go inside and I can actually say that variable test is going to add one every single Loop and then I can simply Echo out something so we can actually see something going on inside the browser here which could for example be variable test just so we can follow what exactly it’s do doing and then I can go ahead and refresh the browser and as you can see we get numbers so it is looping through five times because we had our variable test starting at 5 and then we just add one each time until we get to lesser than 10 uh so it is doing something here so anything that you might want to think of that you could for example use inside an if statement when it comes to checking for these type of conditions is something you can use inside a while loop whereas for example our full loop up here is more about when it comes to like numbers so two different ways to Loop things out you can just kind of like use the one that you might find appropriate for a certain situation to spit out a bunch of data a certain number of times uh but what we can also do is I can also go ahead and do something called a do while loop because right now if I were to go ahead and say that for example variable test is equal to 10 which means that immediately the first time this is actually going to be false because this is not going to run a single time so so if we were to actually save this go inside and refresh it you can see we get nothing inside the browser hm and that is because we started out with a false statement so it’s not even going to Loop out one thing inside our code but what we can do if I can find my mouse here for some reason is really difficult in this background color here uh what I can do is I can go inside and create something called a do while loop and the way that works is I can actually replace the while statement and the parenthesis with a do keyword word and then afterwards down here I can actually include my while statement so I can go ahead and say semicolon and what this is going to do is that it’s going to Loop this code in the same sort of sense as before but it will always Loop this out at least one time no matter if this is going to be true or false the first time uh so no matter what happens this is always going to spit out something one time so in this example here even though variable test is going to actually be a false statement inside the while loop it is still going to Echo out variable test one time so if we to do this go back inside refresh you can see we get 10 now let’s take a second example here because we do have the last type of loop that we have inside PHP so right now we talked about full loop while loop and dual while loop but we do also have something called a for each Loop and in order to demonstrate this one I will create a array which right now has three pieces of data so we have apple banana and orange and what I want to do here is I want to Loop one time per data inside the array so let’s say I want to spit out all this data inside my browser what I could do if I wanted to and do it manually is I could go inside and say well you know what I’m going to go and Echo out my variable fruits and I want to grab the first index so I’m just going to go and grab index number zero and then I’m going to go and copy this down you know two more times because we have two other pieces of data so I can also grab number one and number two and if we were to do this and go inside my browser you can see we get all three but you may start to see the disadvantage here because now we actually first of all we need to know how many pieces of data is inside this array CU we need to know in order to manually type them out here um but also we have to literally manually type them out which is not really a good thing uh so what we can do instead is we can actually make our code automatically just know how many pieces of data inside this array and then spit them out and I just want to point out here that a lot of people will look at these Loops here and think that oh okay so the you know the while loop might be the one I have to use the most cuz that one is pretty cool but this for each Loop here that’s about arrays and we haven’t done much in Array so far so we’re not going to use this one that much right however when it comes to learning about databases and actually grabbing data from a database and outputting it inside our website you do need to know how to create a for each because that’s the one we use in order to do this um so a for each Loop is quite important so what I can do here is I can actually create one so we’re going to use it for each keyword parentheses and curly brackets and then inside the parentheses we need to first of all add in the array that I want to actually grab the data from and what I want to do is I want to use another keyword called as and then I want to give it a placeholder that I can refer to inside the actual brackets down there or inside the curly brackets uh that is going to to use in order to grab the data from inside the array so in this case here because I want to just grab one piece of fruit I could actually say that the placeholder is going to be fruit you know a singular fruit so what I can do here is I can save this and then I can go inside deep brackets down here the curly brackets I keep saying brackets for some reason um but I can go inside the curly brackets and I can Echo something out so I could say this is a which is not going to make a lot of sense in this case because apple is supposed to be an Apple I do know a little bit of English grammar but you know we’re just doing a small example here so it’s okay but what I can do is I can say this is a then add in the fruit variable because that is going to be the placeholder and then I want to just close it off here actually let’s go ahead and move everything down to the next line just so we have a little bit of you know neatness going on in here so we’re going to add a break and just like so it is going to go through each of these data and spit it out inside the browser so if we would to save this go back in you can now see we get this is a Apple this is a banana and this is a orange but now what about a associative array because this is a indexed array and we did talk about arrays in a previous episode basically we have something called a indexed array which is where we go in and if you want to spit something out when it comes to an array is you refer to the index of the array so in this case if we want to grab Apple then we refer to index number zero if you want to grab banana we refer to index number one uh but what about a associative array so what I have here is another example of a associative array where basically we go in and we say that the key is going to be apple and the value is going to be the color of the fruit so we can keep doing that banana is going to be yellow uh and orange is going to be orange because you know it’s the same name so what I could do here is I could actually just go inside my browser now refresh everything but if I were to do that it is actually going to be echoing out the values uh because when we use the S keyword inside of for each Loop down here it is actually going to refer to the values inside this array so right now the values are actually going to be the colors that we added in here so if I want to go inside my browser refresh you can see we get the colors in here but what if I want to also get the key because that is also something we can do in order to spit this out inside the website so the way we can do this I can just simply go ahe and copy paste this little arrow up here because we do it the exact same way and go after my fruit and say I want to point to another placeholder which in this case it could actually be the color so if we were to go back down inside my echo I can say this is a fruit that has a color of and then I can go ahead and add in another concatenation so just going to go and add in our color uh value here so I’m just going to paste it in and if I were to go back inside the browser you can now see that we get this is a Apple that has a color of red so we’re also getting the key in this case here and this is a small introduction to how to you know Loop something out inside the browser depending on how much data you might have inside a array or you want to Loop something based on how many numbers there are or based on a condition like for example with an if statement uh so we have four different types of Loops that we can use depending on the situation that we might want to find one of them useful so in this case here we have something to work with so with that in the next video we’re going to start talking about databases which is going to be very exciting I know it doesn’t sound exciting cuz database like it kind of sounds a bit dry um but when we start using databases with a website using phsp that is the moment where we really start to see something happening with PHP inside our website so that is going to be very fun to do uh so with that said I hope you enjoyed this lesson and I’ll see you guys next time [Music] So today we’re going to do something very exciting because we’re going to start talking about databases and how to handle data inside a website because when you start doing that that’s the moment where you really start to make a website more Dynamic rather than making a static website which is something that basically means that you have a website that doesn’t really change depending on which user is watching the website so when you have a dynamic website all of a sudden you can start changing content or if you were to hand off a website to a client they can start changing content themselves if you want them to be able to do that that is actually something that I don’t think a lot of people realize that when you start learning how to make websites and you want to become a web developer and you think to yourself okay so I’m going to start making websites for people but when you hand over a website and they have to make a change to let’s say a title or paragraph or maybe they want a image updated or something then all of a sudden they have to contact you because you can’t give them a website and then teach them eight to Mons so they can change things themselves um so that doesn’t really make a lot of sense right so you have to be able to make a website that is dynamic where can allow people to change content themselves without having to know HTML or css in order to do so for example if you want to create a blog inside a website and you want the client to be able to you know upload a new blog post by thems without having to contact you every single time that is going to be an example of something we can start doing when it comes to learning phsp together with a database so with that said let’s go and talk about how to set up a database and what exactly it is databases sound so dry when you say it out loud like that uh it’s not it’s quite fun once you get into it and when you really start seeing content change it’s gets a lot of fun okay so the first thing you want to do is you want to make sure that when you go inside your xamp that we installed in the second episode I do believe that you do have both the Apache and the MySQL server running because the first server here which is the Pache server is going to be the web server where we have PHP running the second one is going to be the actual database server so this one has to be running in order for us to access our database with the xamp that we installed uh because you do have a database installed as well other than just PHP and a server so this is something that you do have already uh we don’t have to do anything special in order to set up a database we just have to open it basically just a small quick tip for you because I don’t think I mentioned this in the first episode where we installed xamp uh you can actually go inside the config menu here and you can actually make it so that the software Auto starts these servers when you open up the program so you can just tick this off right here and then you can click save now if it gives you a error message it is because you need to run this uh software here as administrator so the basic way to do that would be to go inside your exam installation go down find your examp uh Dash control right click properties and then B basically just go in here inside the compatibility and set this one to run this program as administrator if you do that then it’s going to allow you to to set these up as auto start uh without giving an error message so with that done let’s go and talk a bit about something called a relational database management system rdbms for short you don’t have to memorize that it’s just so you know okay so we have many different types of database systems out there which is what we call a rdbms um and the one we’re using is the one called MySQL which is also the most popularly used one when it comes to databases with websites especially if you’re using PHP but there are many different types of database systems out there and that you know some people watching these tutorials may be using something else but if you installed exam you will be using MySQL for this tutorial here most of the time if you do actually have an online server from a hosting company you will also be using MySQL just to mention it so it is the most commonly used one which is also why we’re going to be using that one for these lessons here and just to debunk something here because I know I will get some comments asking about this because it is something I’ve seen on previous videos MySQL servers are not the same thing as MySQL PHP functions okay so before people start typing in comments that MySQL is outdated and it’s unsafe to use then it’s not the same thing as PHP my SQL functions okay it’s two completely separate things so we don’t need to worry about using a mySQL database okay that is something that is very commonly used and it’s not unsafe uh in any sort of way um so what I’ll do is I’ll go Ahad and open up my uh browser and inside my browser I can go up and type Local Host just like we do when we actually want to enter a website but instead of saying forward slash the name of the website in this case I’m going to write PHP my admin and if I type that and click enter you’ll see that we enter our database system so PHP my admin is going to be the dashboard that you’re going to use in order to manage your database and you can actually see that we have many different types of databases on the side here uh you’re not going to worry about these ones that you have over here you may not have PHP tutorial because I actually created that myself at some point but don’t worry about these databases that we have over here you just need to know that any database you create will be over here in the the side so what you can see is we do actually have quite a few TS up here uh you don’t need to start freaking out about oh no there’s so many things going on in here um because we’re not going to be using all these tabs up here do also keep in mind that you do actually see some information about your web server so you can actually see what PHP version you’re using so right now I’m actually using 8.2.0 um so this is actually some information about your server the first thing we’re going to be doing is we’re going to create a database for our tutorials here so what I’ll do is I’ll go up to databases in the top here and then you can see we can type in the name of a database so we can just come up with any sort of name of course a name that makes sense as you can see with these databases down here there is kind of like a naming convention so don’t use weird symbols or something like that uh so what we’ll do is we’ll just go ahead and say my first database just so we have something right then you can just go and click create and then you can see we have a database added to the side over here now as a default it is actually going to be selected once you create it the first time but you can also see we can swap between them and actually see some of the uh other databases that we have here so I can actually click back and forward uh but for now let’s just go and select the one we just created called my first database now inside of here you can see this is quite empty like there’s nothing going on in here cuz we just created a completely empty and fresh database and what you can do in here is a couple of things first of all you can import databas base data from somewhere else if you have a existing database that you want to import in here or we can just go ahead and create a database from scratch we will be doing the lad because we will be creating things ourselves and there is a couple of ways we can go around doing that uh I do want to point out to you that right now it says no tables found in database the first thing you need to know about a database is that we have data inside a database so for example let’s say I have a login system inside my website and in order to have that we need to have users signing up inside our website and with that logic we do also need to be able to save that user information somewhere for example the username the password they used maybe an email address maybe what date they signed up inside the website we can save all kinds of information about our user and that is what we use a database for because whenever the website has to remember something inside our website you use a database to save that information about whatever we want to save inside our website and in order to make that a little bit easier we create something called tables because instead of just taking all our data and putting them inside our database there has to be some sort of structure going on so we want to maybe create a table that is called users where we have all the user information we might also have a table called comments so we have all the different comments the user made inside our website inside that table so basically a table is a place where we gather similar information about something inside our website and we do have two ways we can create that either we can go down here and do this in the confusing way at least I think this is the confusing way this is also the way that I see most people do this when they start out making a database uh but you can go down here where it says create new table and you can actually type in a name so in this case I can just say uh users because we use that as an example and then I’m going and say how many columns do I want uh which basically means how many different pieces of information about this user do I want to save so in this case I could say five which is an ID username password the date they signed up an email address you know so we have some some information about them and if I were to click create here you can see we get this very confusing uh table that we can start filling out uh so basically these are going to be the names so like I said we can have an ID we can have a username we can also have a password you know we can start filling in the names for all these different columns what is what they’re called inside our database um but the problem here is that there’s a lot of information that you don’t really need right now to know about um so this is only going to confuse people even more uh so what we’ll do instead is I want to go back so we can go back inside where it says structure and then you can see oh you have unsaved changes are you sure you want to leave um yeah let’s let’s not worry about this right now what we’re going to do instead is we’re going to go and create all our data using SQL code so we’re going to to do everything manually which is also going to be very useful because once you start talking about how to uh change our database and and talk together with the database using phsp from our website directly uh you will need to know SQL code in order to do that so there’s no better place to practice SQL than directly inside a database so hey let’s do that instead so what I’m going to do is I’m going to go up here in the top where you can see we have something called SQL do make sure that your database is selected otherwise this is not going to be SQL code that you write for that particular database because I sometimes see people who have another database selected or no database selected so make sure that your new database is selected and then click SQL now SQL code is actually quite simple to write a lot of people find a little bit intimidating to start with but it’s it’s actually quite simple and and of all the SQL that you may look up and and try to learn uh there is typical SQL you know like very few lines of code that we use over and over and over and over again so it’s not like you have to learn a lot of code just like if you had to learn PHP for example it’s it’s quite simple and there’s not a lot of code you have to learn for now but this is the tab that we’re going to be using in the next video when we actually start learning how to create a table together so we’re going to take this video by video we’re just going to do one step at a time uh chronologically as we’re making this database uh really really what we have to do here is just create a table and that’s pretty much it for now but although we could take this table we create in the next video and use it directly inside our website I do want to use this particular SQL uh editor here to just kind of show you how to do various things using SQL uh because you’re going to be using the exact same SQL inside your phsp code so the best place to practice it is doing it inside this database here I do also want to point out that even though we are going to have a couple lessons here where we’re not really going to write any PHP code and this is a PHP course this is the other side of the coin when it comes to learning PHP so if you don’t know this stuff here then you’re missing out on half of what PHP can actually do in order to make a website really cool so learning about databases and learning PHP with it is something that is necessary when you’re learning PHP for the first time so this is going to be a couple of lessons of just SQL programming essentially uh but it’s going to be very worthwhile to to learn so with that said I hope you enjoyed this lesson and I’ll see you guys in the next [Music] video so now that we have a database inside PHP my admin we need to talk about how to set up a table inside our database or multiple tables since we can have many tables inside our database and in order to do that we’re going to program this into our database using SQL code which is something that stands for structured query language which means that we’re quarrying the database using this code here in order to manipulate the database and you know maybe create tables or insert data or select data or delete data or you know there’s many things we can do using SQL and SQL is something that is not really too difficult it takes a little bit of memorizing to do but a lot of the code that you write using SQL it’s going to be pretty much the same so it’s not really like learning phsp where you have to memorize a lot of different code it’s it’s more about learning the same queries so to speak in order to manipulate the database so the first thing we need to do here is make sure that our database that we created in the last episode is selected over here in the sign so you need to make sure you click it otherwise we’re going to be writing SQL code for something else outside you know on the side here we don’t want to do that cuz we want to make sure we select our database so with our database selected you can go up here in the SQL tap and in here we’re going to go ahead and start writing some SQL code so we can actually write some you know some instructions to our database for it to do something like for example creating a table so I hope this is big enough because it is quite tiny I mean I guess I can zoom in a little bit more so you can see a little bit extra here just just to make sure that you can actually see what is going on cuz it is quite a a tiny console that we can write into here so now when it comes to creating a table there’s a couple of different concepts that we need to talk about before we get started here because we do have uh data types that we can insert inside a database we do also have something called signed and unsigned we do also have some different commands like autoincrement which means to we automatically assign numbers to a certain column uh we do also have columns that we need to talk about you know we have columns and rows uh which is like columns is the uh the vertical and then rows is the horizontal you know we have some different concepts we need to talk about but what is important to know is that this video is kind of going to be a mega video it is going to be a little bit longer than other videos because there is like I said quite a few things we need to talk about but you need to see this video as something you can return to so if you need to know a little bit of information about for example what a row is or what a column is or what signed and unsigned means what does a primary key mean what is a foreign key you know then you can come back to this video and then it will be time stamps below so you can kind of like fast forward so you can you know find a specific part you need to refresh your mind with in order to know exactly what that was but just know that I don’t expect you to memorize everything on the first go in this video here we will get to do more code in the future when we actually get into PHP we’ll also write SQL code so you know memorizing these things is not something I expect of you on the first goal so the first thing I want to talk about here is data types because when it comes to inserting data just like with PHP we also have you know integers we have characters you know like actual writing like text and that kind of thing we have dates we have uh decimal points that kind of things so we need to talk a bit about the different types of data we can insert into different columns because when we create a table we need to define a row which is all the data we have for one entry and then we have the columns which is each individual piece of data there you might have a one database entry so essentially we would have for example a person so we would have you know first name last name age you know hair color that would be like the different columns and then will we grab one row from the database that is basically grabbing the entire person with all these different attributes that that person has so we can actually pull the row out and display the data inside a website for example so we have you know columns and then we have rows and each column inside a table needs to be defined with a data type in order to tell our database what kind of data do we expect to be put inside this column here so for example if we were to go in here and say that I want to Define a integer data type then we do have an INT data type which is basically when we have four bits of data we can insert as a number inside this column here which is not a decimal point so you know just like a regular number like this and one thing to note here when it comes to these different types of data that you can insert inside a database is that they all have certain amounts of bits you can store inside this specific column here so for example when it comes to a in we can actually store a number that goes from I actually just went ahead and wrote it out here because it’s going to take a little while but when it comes to a integer data type we can store up to four bits inside this specific column here which means that we can store up until minus this number here until positive this number here so this is going to be four bits I’m just going to go and leave this for now because we do also have something called called signed and unsigned we need to talk about but this is basically the integer range that you can store inside a integer column if you want to choose a integer data type so let’s say for example I want to have a much larger number inside my um my column here because this might not be enough for my specific number let’s say I want to store a uh money amount inside my database and maybe that money exceeds this amount which in that case you’re a very lucky person if you have that much money um but let’s go and say I want to store even more money inside this database column what I could do is I could also use something called a big int and it is important to note here that we do have something called tiny int int big int um so we have many different types of integer data types they can store up to you know different types of bits inside of it but for now we’re just going to talk about the more common data types that they might want to use inside a database cuz I it is going to be quite a long lesson if we’re going to list them all out so let’s just go ahead and stick to the the ones that you might want to be using at some point then you can by yourself look up some of the other types if you want to at some point in the future but for now we have something called Big end and this can store even greater numbers so in this case here I’m not going to write it out but this is going to be up until eight bits of data or eight bytes it’s called the previous one up here is also four bytes by the way not bits um so essentially what you can store here is a much larger number which again is also going to range from a negative number to a positive number so it could for example write something like this inside of it and it would actually be able to store it because as you can tell this number is much bigger than this number up here which is the maximum for a uh int data type so you know we can store a lot bigger number when it comes to uh the big int down here and you might be asking well Daniel so if you’re saying that this one can store a lot bigger number than this one why don’t we just use this one every single time and essentially it all counts down to Performance and that kind of thing because we want to make sure that we don’t uh take up more space than we need to if we don’t need to get close to this number here but this is plenty for storing you know a certain type of data inside our database then performance will be better if you just stick to the data type that makes sense for that particular purpose so don’t just go around using the the biggest data type that you can find so in that case you can always store the biggest number um so so choose the one that makes sense for the the circumstance you might want to use it in and now we do also need to talk about something else before we move on to characters and decimals and date times and that kind of thing uh we can also go ahead and put parentheses Behind These data types and actually Define a parameter for these data types here in this case here when it comes to integers it is a device with that we’re defining in here so if I were to write something like 11 um this does not mean that I’m allowed 11 characters like 11 numbers inside this integer this means that the device with allowed for this data typ type is 11 numbers and this is not something that is directly going to be affecting your website this is something that is going to affect certain types of applications that is used to for example show data for example our uh MySQL software here U has a console inside of it where we can actually show the data from inside these different columns so if I were to set this one to five and I were to store let’s say this number right here then I wouldn’t be showing this number right here inside my database I would actually be showing this number right here although this number is actually what is stored inside this column but it’s not shown inside this database because we defined a Max limit for how many uh how much the device width is going to be when it comes to showing this data inside this database here and like I said this is not something that really affects your website when you define a number in here because you can still show the full number inside a website uh so a typical thing that we do when it comes to just making websites is we just Define 11 in here as a default which by the way is from what I remember also the default when you just write this it is going to be the same thing as writing 11 inside the parentheses so this is not really that important if you’re just a web developer and you just want to store numbers that you want to show inside a website but just out of habit we’re going to go and write parenthesis 11 to make sure that it is defined because some database types may be some Legacy databases or applications that needs to show this data do need to have this defined so it is better to have it than not to have it okay so with that said let’s go and talk about a float number so we can also Define a float which is of course also going to be when you have a decimal point so you can for example say you have minus something you know this number right here dot something and you can of course have a Max and a minimum that you can store inside when it comes to bytes uh in this case it is also going to be four bytes which means that we can store I’m not going to type it out here cuz it is you know a long number like these ones up here but essentially you have a minimum and a maximum which is equal to four bytes that you can store inside this particular column here uh the same thing goes when we have something called a double so we have something called a double which can store much larger decimal points so just like we have up here a inch and we have a big int so we have one for storing you know a normal amount of numbers and one for storing a large amount of numbers the same thing goes for float and double this is a small amount of numbers and then this one down here can have a lot greater amount of numbers inside of it when it comes to like decimal points and that kind of thing so uh we do have different data types again just like with numbers we have the same thing when it comes to decimal points um and then we do also have something called vaa now Vara is when we have to do with characters uh so let’s say I go in here we can also create the parentheses which is actually something you should do cuz the default is going to be one uh which is this right here and that is not really useful for anything cuz that means we can store one character inside this column here um so it is you know you should create parentheses outside vaa in order to define something here so in this case here we could for example say 10 so what this would basically mean is that I can store 10 characters as a string inside this data type so if I were to write Daniel uh this would be able to be stored in there but if I were to write something like Danny cing which is going to be more than 10 characters is going to cut off the last parts of this string here because we only allow 10 characters inside this column here so depending on how many characters you want to allow inside this column here you need to change this number in order to allow for that amount of characters now when it comes to something like let’s say a username you could say something like 30 cuz you know 30 characters for a username might be plenty so you don’t want to be able to store more than that so we do have different numbers we can provide in here depending on the purpose of what we want to create inside our database if you have a password uh then we can also go down and say maybe something like um 255 because we want to be allowing many characters for a password but I have rarely seen anyone use more than 255 characters when it comes to using a password U so of course you know you can Define different things depending on the purpose that you want to allow the person to insert inside this particular column here and just like before we do also have a certain number of bytes you can store inside a v chart data type so in this case here it would be in the range of something like and again this really depends on the database because some databases only allow for 255 which is why I use that one as a maximum um but you can also in some database do up to 65,535 bytes so you know it really depends on the databased type you’re using in our case I do believe we can store many bytes inside this uh inside our database here but 255 is kind of like the typical number that you define inside a vacha data type because then you know that it can be stored in many different types of databases because this is the maximum for some databases uh so 255 is also plenty in my opinion for storing U you know characters because you know we have other data types of storing much greater number of characters for example we do also have a data type called text and just like before when we talked about int big int float and double if you want to store many characters inside a column you can just use something called text which is going to just be defined as text and then you can store for example a blog post or a big comment for post inside your website that has a lot of characters and text inside of it so uh using text for for example comments and blog post and that kind of thing is kind of what we need to use this data type for whereas vacha might be something used for usernames and P password you know something that’s not a huge paragraph that needs to be written inside your website so again choosing the proper data type for a certain type of data or usage inside your website that is appropriate to the amount of characters that you might be using is something you should be doing but now the last type of data type that I want to talk about here is something called date and date time so we have something called Date here which is basically when uh you want to create a date for a certain moment in time so for example today it is the year 2023 and it’s May month which means 05 and then it’s the 14th of this month year uh so we do have different types of uh formatting when it comes to our date and date time and that kind of thing so the formatting is something we need to be aware of when we upload a date to a database because if we don’t format it correctly it is not going to get stored inside a database so when you write PHP code you want to make sure you format it in the same way as the databas is so with we do also have something called date time so we have date time and that is basically the same thing so we can actually copy paste this but we do also have the time so if I were to go in here and say that right now it is 11:30 in the middle of the day so it is the 11th hour and then we have the 30th minute and then we have the let’s just say the zero second of the day uh so this would be 11:30 and then of course we go into not a.m. and p.m. but we’re going into for example uh 17 if it’s 5:00 in the afternoon so we go after this time format here so these are the different data types that I want to talk about but I do want to talk about something called signed and unsigned when it comes to using numbers up here because let’s say we have this integer example here let’s just go and delete everything else now we did talk about there being a certain number of bytes available inside these different data types that we have in here and when it comes to a integer data dat type we have four bytes available to us and that basically means that we can range from this negative number here to this positive number here however if I were to go in and say I want to Define my integer as signed this right here means that we can actually store a negative number inside our database column as well so this would be uh this range that we have right here but I can also go in and Define this one as unsigned which means that we don’t want to store negative number numbers inside this particular number column and that opens up a little bit of options for us because right now we only have four bytes available which means that this is the maximum positive number we can use but when we cut out the negative numbers we can store from zero until a much greater positive number because we have more bytes available and now we cut out all the negative numbers which means that we can store up till actually just went ahead and Googled it here CU that was a little bit easier but you can store the twice as much number when it comes to the positive number so inside a column you can define something called signed and unsigned when it comes to numbers that goes down into the negative range because we want to maybe not allow negative numbers to allow more spacing for positive numbers so that is something that is just important to mention here so now that we talked about all of this let’s actually go and talk about how to actually create a table because now we got a lot of dry knowledge about data types and signed and unsigned and you know defining parameters and you know bytes and that kind of thing so let’s talk about how to actually create a database table so if we were to go in here we can define a create table keywords it’s called which basically means that we’re going to create a table named something so in this case if we can name something called users if that’s how we want to create a table called and this of course would be information about a user inside your website so for example username and password email you know the date that you signed up inside the website that kind of thing there is something to note here which is if you were to write this you may notice that we get a popup which says that this right here is a existing keyword inside our MySQL keywords here so we don’t want to be using a existing keyword so this right here is a no no okay so we want to make sure that we write something that doesn’t pop up as an existing keyword otherwise you can end up in a little bit of you know errors and that kind of thing so make sure you don’t do that so one of the things that I do when it comes to naming a table is to say well inside a table we have many entries of data right so we have many users inside this table which means that it makes sense to call it users in plural because we have many uses inside this table here so I like to name mine users and it is allowed to use a underscore for something specific so if you want to write something you can use an underscore but in this case I think uses is good enough parentheses and semic one and then I’ll open up the parentheses and then we can start creating all the different columns inside this table here in between the parentheses so now we need to think about what kind of information do I want to remember about a user inside my website when it comes to a you know database and information kind of purpose the first thing I want to do here which is something you need to do in pretty much all the different tables you create is to make sure you have something called an ID so I’m just going to go and name this one ID and then I’m going to define a int data type and like we talked about we don’t really need to have something behind inter but for the sake of making sure the Legacy databases and that kind of thing can can use this data for something let’s go ahead and make sure we Define 11 in here just to to make sure we have it defined even though it won’t really matter to us in most cases as a web developer we we should still Define it in here uh so what I’ll do is I’ll go after and say that I want this data type to be not null which means that we don’t want it to be nothing uh that’s basically what null means it means nothing it is different than not having any sort of data it just basically means nothing which is a concept we have inside programming um but basically this means that we we need to have data inside this particular column here and what I’m also going to do here is I’m also going to define something called Auto unor increment and I know I’m not really explaining all of this right now so let me go ah and explain it right after this line of code here so basically whenever we have a table we want to make sure that we can find data inside this table very easily which means that having a ID for all the different rows we have inside the table is going to make it a lot easier for us to grab certain data so let’s say I have a user that has an ID as 65 for example if I were to look inside my database table for the user that has an ID at 65 which by the way he’s the only user who can have that number then it’s a lot easier for me to find that user inside the database than trying to search for his username or something so whenever we’re trying to search for something inside this table here having an ID for all the different rows inside the table is going to make a lot easier for us so basically what we’re doing here is we’re saying that we want to have an ID for all these different users that is going to be a unique identifier that is what id stands for and I want this to be a number data type and I want to make sure that there is a number for all the users in here cuz this cannot be empty and now in order to not manually having to write a ID for every user whenever we create a user inside our website I added something called autoincrement which means that our database is just going to handle this ID column by itself and automatically add a number to this user whenever we create a new user inside this table here so user number one is of course going to be user number zero and then the next user is going to have an ID is one and then number two and then three and then four and then it’s going to keep counting up every single time you add a new user to the database so this is automatically going to increment a number hence the autoincrement keyword and this by the way is unsigned which means that it’s going to use the positive numbers and not negative numbers so this is automatically also going to declare this as being a unsigned and this is something that you have to remember this particular line of SQL code because we use this pretty much in every single table we create inside our database because all our entries needs to have an ID so having this particular line of code as the first thing is going to be very very important now we do have something called a primary key that we need to Define inside this table here which is going to be this particular number but we’re going to wait with that until after we declared all the columns because I want to talk about it afterwards uh so for the people who do know a little bit of SQL here just we’ll get to it okay um so the next one here could for example be a username so let’s say we want to declare a username for this user so we can say username and I want this to be a v chart data type because this is of course course going to be a set of characters because we need to have like you know Crossing or something you know as a username uh so what I’m going to Define here is just 30 because I want to allow 30 characters inside this username more than that I think it’s going to be you know overdoing it a little bit so if a user tries to declare something bigger than this then it’s just going to get cut off at the end there because we don’t want to have more than 30 characters you could also say that maybe like 20 characters is plenty uh but let’s just go and go with 30 for now uh we do also want to declare this one as not null cuz I do want to make sure there is some data inside this column here and then I want to go down to next line and we can actually copy paste what we have here cuz the next one is going to be very similar and this one is going to be the password now there’s a reason why I don’t write password and you may see it there because there is a built-in keyword called password so instead I’m going to declare a PWD which is kind of a short hand for password and doing that I do want to allow more than 30 characters and when it comes to passwords we don’t want to really limit our user when it comes to a password we want to allow them to create as long as a password as they want for security reasons cu the longer it is the better it is but let’s not go overboard here let’s go ahead and say we want to allow 255 characters which we talked about is kind of like the maximum number for some databases so let’s go and make sure we Define uh 255 in here which is plenty for password I think so we don’t need to have more than that uh after this one we can go and go down to the bottom and maybe say something like email and let’s go and Define a vaa as well so we’re just going to copy paste here and I want to allow something like let’s say 100 characters cuz I think you know an email of more than 100 characters doesn’t really exist out there so let’s just go and Define 100 characters and for the last column here let’s go and Define a date time for when they us to sign up inside our website so we know when they signed up inside the website so what I could do is I could create a column called something like created underscore at just so we know you know we have some kind of name for this column here you can call whatever you want in my case I’m just going to call it created at and I’m going to call this one a date time format or data type so to speak and inside of here what we can do is first of all Define a not no which means that it should not be empty and we can also go and Define something called a default which means that if I decide not to write something and send it to my database table here to create a new user then if left empty it is going to by default assign something by itself to this particular column here so what I could say is I could use the keyword called default and actually assign a default value so in this case I could say you know just to give an example here that we have a year a month a day uh we do also have a hour we have a minute and a second and this would be a default date time uh that I could set for this particular database here but of course this doesn’t really make sense like this is you know the year zero at the zero hour so basically the creation of the universe or something so this doesn’t really make sense but what we do have is another keyword inside the database here called current and then you can see we get date and time so one here is just the the date format and the other one is the date time format so we can say current time which means that it’s going to automatically create the time based on the server time so this basically means that we don’t really have to define a date time using PHP if we don’t want to we can do it because this is just a default which means that if we don’t submit something then it’s just going to assign the current time based on the database server but you can also submit a date time using PHP if you want to so you have kind of like an option now but now we do also have something called a primary key and the way you could Define that is by for example going up inside the one column that has to be the primary key and just write primary key inside this this particular column here and now this is defined as a primary key so this would be correct but it is also just kind of a habit to go down here at the bottom and say that you want to have a primary key so we can see we have primary key and we want to define a column so we can say parentheses and that is going to be the ID column which is the one that we have up here and doing this here is also a way to do it so that is just a second way of doing it instead of writing it directly inside the actual column up here um and do note that I’m not writing a at the end cuz the last line of code inside this particular SQL statement here inside the parentheses has to be without a comma so all the other ones you need to include a comma but for the last one you leave it out otherwise you’re going to get an error message now a primary key is basically just a way for us to Define which column inside this table is going to be the one that we use that has to be unique so it can’t be duplicated elsewhere inside any other rows inside this database so for example we can’t have one user that has an idea 65 and then another user that also has an idea 65 because then we start getting error messages so primary key is just basically a way for us to create a unique ID for all the different users in here so we can very fast just grab a user and you know find them very fast inside our database so now with this created we actually do have a database table we can just run inside our database and actually create it uh let’s just go ahead and out of habit just copy everything because because in case something goes wrong and it’s always a good idea to have this you can also go and store this information inside a empty text file or something so you have you know all this data somewhere so in case something goes wrong and oops okay so this particular one up here could not have been named username so I have to call it user uncore name or something instead then instead of having to rewrite everything you have everything stored somewhere so that is a recommendation that I have so you have it saved essentially um so what I can do is I can scroll down and then I can say we want to go which means that we’re going to run this inside our database and then you can see we now have our first database table which is called users if I were to click the database you can see okay so we have our users table right here and we can click it or we can click it over here in the side and then you can see we have an ID username password email and created ad inside this column here or inside the table it’s called um so right now you can see we don’t actually have any sort of data otherwise we could see it below here you just listed out we’re going to talk about how to insert data and update and all that stuff in the next episode but for now let’s go and create another table uh because we do also need to talk about something called a foreign key so again we want to make sure the database is selected and because I made everything so tiny by zooming in it’s now a burger menu for some reason if I would to do this you know to zoom out you can see that I have it up here uh but basically we’re just going to go and click the SQL tab once more I’ll zoom in again so you can see it and we’re going to go and create a second table so in this case I’m going to create a table I’m going to call this one comments because we’re going to create comments that the user can make inside a website underneath something so a picture or a video or something like that so we’re just going to go and create a comment table parentheses and semicolon and what I’ll do is of course I need to Define an ID cuz that’s the first thing so we’re going to say we have an ID this is going to be a int data type and just like before we’re just going to set 11 as a default one and we’re going to say this one is not null we’re going to say this is auto increments so Auto uncore increment so it automatically assigns a new number comma next line and then I’m going and say okay so what do we need to know about this comment here we need to know U for example the username of the person who made this comment so we could say username and we can say this is a v chat data type we’re going to define a username length which in this case it could be 30 because that’s what we defined in the previous table so we’re just going to say 30 here as well not null and then I go down to next line and we do also need to have some text in here of course cuz you need to write a comment so there has to be text in here right so instead of using vaa for this one because we want to allow for the person to write many characters inside this comment here we could also use a text data type so let’s go and say we want to have the comment uh message or just comment text let’s go and do that cuz that’s a lot shorter so comment text and I can say this is a text data type I want to set this one to not no and I want to go down to next line let’s also go and create a timestamp for this particular um when this was created so we’re going to create a datetime data type and we’re going to call it something like creatore at cuz again we can just reuse the same name as the previous one um and then we’ll also go and Define a not null and we can also go and create a default and then we can again use the same thing so we can say current time stamp or current uh time it’s called so it will just automatically create a time stamp for this particular comment once it’s actually created and then at the end here we’re going to create our primary key so we’re going to say we have a primary key which is going to be our ID so we’re going to Define ID inside this particular one here now we want to also Define a foreign key in this example here cuz we can create something called a relationship between one database table and another database table so right now we have a user who made this comment so we’re trying to think logically here which means that we can actually tie a comment to a particular user and you might be thinking well Daniel we defined a username isn’t that what you’re saying so if we say that a user called cing from the users table uh now is also named Crossing inside this comment table here aren’t those connected then um to a human that might be connected but for a datab base that is not connected uh there’s no relationship going on between the two tables so what I can do is I can actually say that okay so this comment here let’s say we actually upload a comment to this table here has to be connected to a user that has a certain user ID again the primary key inside the users table has to be connected to that particular comment and there’s a couple of reasons why we can do this let’s for example say I go inside my website and I sign up as a user and now I have a a user account then I go inside a picture and I write a comment underneath the picture now what is going to happen when I delete my user account is it going to delete all the comments inside the website that I made with that account or is it just going to throw me an error message because hey you made comments inside the website so you’re not allowed to delete your account or should we just go ahead and say that okay so we can delete the user account but we now need to Define all the comments the user made as a deleted user because the user no longer exists inside the website so what needs to happen once we have this relationship and you decide to lead a user from inside the website so what I want to do here is I want to first of all Define a column that is going to reference another column inside my users table and when it comes to naming this column here we can give it any kind of name that we want to but in my my case I think it makes sense to say that this is from the users table underscore and it’s going to be the ID for the users table so just a little bit of logic here for me so I know exactly what this is and what I can do is I can define a data type and not null and all this kind of thing um but it’s very important that when it comes to the data type that we assigned the same data type that is inside my users table for this particular column that I want to reference to so in this this case here this is the ID for inside my users table which means that we have the same type of data as we have up here which is int 11 so we need to make sure that we Define the same data type for this particular column here then I can also go and say not null and what I can do now is I can go down below my primary key and I can define a foreign key so we can say foreign key the first thing we need to Define here is which of my columns inside this particular table is going to be my foreign keys in this case here I want to say this is my users ID this is the one that I want to reference to a key from another table so what I need to do is write references because we reference another table called users and we point to a column inside this users table called ID because that is the ID from inside the users table that I’m trying to reference to inside this column here and now with this we could just go ahead and submit it and that would be okay everything would work perfectly fine um however like I talked about previously if I were to go inside my website and create an account and write a comment underneath a video or a picture or something and I then delete my user account what is going to happen now now as a default you’re going to get an error message because as a default we have something called on delete and then we can write something like no action and what this basically means just to kind of explain this for you is if I were to go inside and delete a user from inside my users table that has a certain ID that is connected to a comment that is made inside this table here so right now there’s a relationship going on between a comment and a user from inside the users table if I were to delete the user so on delete then I want no action to be made so essentially it’s going to throw an error message and not delete the user because we don’t want to delete the user because there’s a relationship going on here so if we delete the user then it’s going to break our database table relationship and this is basically the same thing as doing this right here because this is actually the default state of what is going to happen if you try to delete a user that has made a comment so that is not really going to make a lot of sense in most websites like logically in the real world this is not going to make a lot of sense because you know I should be able to go inside a website and delete my user account even if I made a comment somewhere like that should be a valid thing to do uh so we do also have something called Cascade so on thead I want to Cascade which basically means that if there’s any comments inside this table here that has a relationship to a user from inside the users table that is now being deleted then also go ahead and delete all the comments made by that particular user so that is what Cascade is going to do but we do also have one called set null which is basically going to instead of you know deleting all the entries or throwing an error message this one is just going to go ahead and set this column in here as null whenever we delete a user that this one has a relationship to which basically means that there’s not going to be any sort of ID that we’re pointing to inside the users table it’s just going to delete it from inside this column here but now if I were to try and run this code inside my editor here so let’s go and make sure we save it just to make sure if we were to scroll down and actually run this you’ll notice that we get a error message message oh no there is a error which is called error number 10005 which is actually when it comes to the foreign key being written wrong inside this table here now if you know a lot about logic inside a table you may see what is wrong here because right now I’m telling it if I were to delete a user from inside the users table then go ahead and set the comment column which is called users ID to know right but what did we also Define inside this column here we said it cannot be null so right now we’re saying okay so if we delete a user set this column to null but hey oh we’re not allowed to do that so if I were to do this down here I do also need to make sure that we don’t have not null inside this column here otherwise we cannot do it so we need to make sure we do this in order to submit it so now that I have this again we can copy everything go down to the bottom make sure you have no comma at the end here by the way and go ahead and submit this then you can see everything works out just fine so now if you were to go inside my database you can see oh we have two tables now we have a comments and a users table and these actually have a relationship with each other we have a foreign key that points to another table so now we actually have something going on here and with this we now know a little bit about creating tables inside a database or you know a lot about creating tables inside the database of course you can go much deeper into it uh but for now I think as a beginner sort of lesson that is already quite long this is going to be plenty for teaching you how to create tables inside a database so for now this is what you need to know about creating tables and in next video we’ll talk about how to write some SQL code to just insert and select data it it’s much more simple than this episode by the way um this episode was quite long and complex compared to the next episode because we just need to write like one liner code in order to insert data into a database you know into one of these tables here so that is going going to be much simpler okay so with that said I hope you enjoyed this lesson and I’ll see you guys in the next [Music] video so now that we learned how to create a table in the last episode I thought it was a good idea to talk a bit about how to actually insert data inside our database table since right now we have these two tables that we created but we don’t really have any s of data inside of them we’re not going to talk about selecting data in this episode here since we also need to talk about something called joints in order to talk properly about selecting data and that would mean that this would actually be a little bit longer episode so for now let’s just go and talk about inserting deleting and updating data since it’s fairly simple to do so we don’t really need to have a long episode to explain that with that said as you can see right now I selected my database and inside our database we have comments and a users t table since we created that in the last episode now what we’re going to do is we’re going to talk a bit about how to insert data to begin with so I’m going to go inside my SQL tab up here make sure you do have your database selected over here on the side and from in here we’re going to go ahead and write some SQL that is going to insert some data inside our users table just so we have something to to work with you know to teach you how to insert data so what I’m going to do is I’m going to write something uh called a insert into statement which basically means that we’re telling it to insert some data inside one of our tables and in this case we want to insert into users which is our users table and then we want to tell it what exactly do we want to insert inside this table here so in this case here we do have a couple of table rows that we want to do something to and when it comes to inserting a row of data we do have different columns that we need to provide some uh some data for uh so if I were to actually right click on users and open that in a new tab so we can actually see our users table you can see that right now we have an ID a user name a password an email and created at so those are the five columns that we need to insert data for now in the last episode we did talk about ID being automatic so we didn’t have to touch that one it’s just going to automatically create an ID and we did also create a default value for our created ad so we don’t actually need to create data for this one either so right now the only one we actually have to do something for is username password and email so what I can do is I can go back inside my insert statement and inside the parentheses I want to tell it which columns do I want to insert data into so in this case that we want to say that we want to insert inside our uh username then we do also have a password and we had a email so right now we have these three different columns I want to insert data into and you have to remember the the order here because the order is going to matter and afterwards we want to say that we want to include some values to insert inside these columns so we’re going to say values which are going to be inside a parentheses as well semicolon and inside of here I want to provide some strings since in this case we do want to insert strings not numbers and if it had been a number I would just write a number in here but because we’re dealing with a string or characters I want to make sure we have these sing quotes you can also use double quotes but it is just kind of a habit inside when it comes to SQL statements that we use single quotes So for that I’m going to go and insert a username so in this case I could call myself cing just to do that I could also provide a password so in this case yeah I could say something like Danny 123 just to you know have something in here uh I do also want to point out when it comes to actually inserting a password you know in more real life examples we do need to Hash the password first so it’s more secure uh because if a hacker were to gain access to our database it shouldn’t be able to tell what the password is so you know doing a little bit of hashing for the password is something you need to do uh but just because we’re practicing here we’re just going to go ahead and insert a password that we can actually see inside the database because we want to see if this works so afterwards here I’m going to go and provide the last one which is going to be an email so I’m just going to say uh John do John do at so John do at gmail . just to you know give it something this is just a placeholder email and just again to make sure we have this saved I’m just going to copy everything here and I’m just going to go and open up my text editor and just paste it in so now we have it stored somewhere so I can actually go ahead and see it in here you know so we we can just copy paste it again if we need it for another time um so what I can do is I can go back in here scroll down and I’m just going to go and submit this one so I’m going to click go and when I do this you can see one row inserted inside our database table so if we want to click this users table or just go into the tab that we have open refresh it you can now see that we have a piece of data so right now we have aids1 we have clossing Danny 123 John Doe and then we have a date for the the time right now that I created this user here so everything is working perfectly so now let’s go ahead and go in and add a second user just for an example here so I’m going to go and go back make sure my database is selected now because I’m zoomed in it’s going to give me this burger menu up here so don’t worry too much about that just go in Click SQL again and I’m going to go and paste them what we had and I’m just going to change the values here so we can say this is going to be Bess and this is going to be just Bess 123 and I’m also going to go and change my email to Bess gmail.com and I’m just going to scroll down and click go so now for I want to go in you can now see that we have two different users us so now we have an idas one and an idas 2 and remember the ID is going to be a unique identifier so in this case here you know each user has a unique number for each of them that is going to be relevant for a little bit later in this video here um so for now let’s just go ahead and save this as it is and talk a bit about how to go in and update one of these tables here so if I were to go back in and make sure we have SQL clicked I can go in we can now create a update statement so if I were to say want to update one of these rows of data um just to go back in here just to show you uh so this is a row of data as you can see horizontally we have all this data here and then we have the columns which is the ID the username the password email and creat AD so you can see how we have rows and columns right just to point it out uh so going back in I want to change one of the rows from inside my table so what I can do is I can write update which is another command that we have here and I need to tell it which table do I want to update in this case here so right now I want to update the users table and I need to tell it okay so we have the user table selected but what do I want to change and from which specific row and this is where the ID comes in because what I can do is I can say I want to set a new value so in this case here I can say let’s say I want to change the username of Bess for example what I can do is I can go in and say I want to change the username and I want to set it to a new value so I’m going to set it equal to a new string but like I said it is more common to use single quote so let’s go Ahad and stick to what I’m saying um so what I can do is instead of saying Bessa 12 three is I can say Bessa uh 456 just to change it to something else now we have you know not one two three but we have 456 if you want to change the second piece of data you can also do that so you can also say comma and then add another piece of data from inside that particular row you want to change so in this case here I can say that I want to change maybe the I just realized this is actually the uh the password I just wrote in here so let’s go and copy this uh this information here and say I want to change the PWD into Bassa 456 because that makes more sense so let’s change the the username into something else let’s call this one Bassa is cool just to have something right because he is kind of cool he’s sitting right over there um so what we can do now is we picked two columns that I want to change and you could pick all of them if you wanted so you can just keep adding commas and add some other data um but in this case I’m just going to go and say I want to change these two different columns and I do also need to tell it where from inside this table I want to select a row because right now we don’t actually know exactly what we’re picking like right now we’re just saying oh set all the usernames and all the passwords to this value but we need to pick a specific user that we want to graph from the the mix of of users so what I can do is I can say where the ID is something specific so I can say where ID is going to be equal to two because if I were to go back inside our users table you can see the Bessa has an ID as to so in this case here I can just go ah and pick out you know the person with the ID of a specific ID is going to have this data changed about it um if you don’t know the ID of the user you can of course also go in and say that okay so right now it’s the person with the username that is equal to uh what was it Bessa um or if I want to say okay so we need to have the user be a user with a username of Bessa but also so and the password should be equal to something else so Bess one two 3 for example here inside strings of course so we’ll make sure we have single quotes there we go um so in this case here we only change the information if Bess has this username and this password here or we can also go in and literally I just made an unintended pun there uh we can also say or instead of and and say that if the username is equal to cing so in this example here I’m changing the username and the password of two different users inside my database so you can also use you know a username search two times in a row row and just say okay so if the user has a username A Bess or crossing then change them inside the database or ID you can also write ID and say ID is going to be equal to uh one so we can actually say this is a integer so we say one or the ID is equal to two so this would also be the first two users inside the the table so you know like we can mix a match things here and and mess around with it as much as you want um but let’s just go ahead and go back to just is saying where ID is equal to one in this case here so if we were to actually run this and go down say okay or go it’s called Uh then you can see if for where to go back inside my users table notice how bassim uh is going to have his username changed and his password is going to change once I update okay so I accidentally set the user ID as one okay so we changed the wrong one here so instead of changing you know the Bas one down here we actually updated my cing one uh because wrote we wanted to change it where the ID is one and not two uh small mistake on my end but hey it it kind of proves my point so now you can see the old version down here and the new version up here so you can see that it is actually changed um so what we can do now is talk a bit about how to delete data from inside the database and I want to give you kind of a small little example here because I get a question quite often when it comes to deleting data from a database and I want to kind of prove a point here so what I’ll do is I’ll go in here and I want to say we want to delete from our users database so delete from users and I want to tell it which row do I want to delete from inside this users table which is going to be done in the same sense as we just did with the update because we have to pick the specific row maybe based on an ID or username or something you know can mix and Max this with and and or just like we did before but let’s just go and say we want to delete the one that has an IDE D equal to 1 which is the one we also just updated so we had Bessa is cool and Bessa 4556 right so if we were to do this and go back down to the bottom and run this you can see that now we get a small popup so it actually says are you sure you want to really execute this quy and I’m just going to say yes here and then we say one row is affected so if we go back inside and refresh you’ll notice that now we only have our second user inside the database table and this is where I’m going to prove a point because what is going to happen when I insert another user inside my table here because if were to go back inside my little uh code that I saved in here conveniently and went back inside and ran a new insert statement so if we were to go inside my SQL tab here and run a insert statement and say we want to insert Crossing Denny 123 you know blah blah blah and run this what user ID is you’re going to get if you guessed one you are incorrect because if we go back in here and refresh you can see oh okay so Bessa has an IDE is two and then Crossing has an ID as three oh no but that messes with my brain cuz where is user with the ID as one inside this table here I do sometimes get comments underneath my video asking about so what do we do in this situation here cuz you know one is missing so so what are we going to do uh can you just go inside and maybe change this so we write one and then we write two down here uh so you actually do this and you actually update it because you can do it like this as well if you wanted to um so if I do it like this ah now everything is matching right if we can actually get this out of the way so now user number one is B and user number two is crossing so ah now it’s not going to be two and three anymore you know so it’s not like weird or something but it’s very important to point out here that doing what I just did changing the IDS manually by just going in and changing them is a big no no okay you cannot do that let’s say for example that one of my users made a comment inside the comments table which now means that one of those users are assigned to a comment and then I manually afterwards go in and change the IDS of all my users then all of a sudden all the comments and which user made those comments are going to get all messed up inside the database so don’t change the ID manually just go ahead and leave the numbers broken as it is because it’s supposed to be be broken if you start deleting users so if it says in this case here I can actually just show you another thing here uh if I were to try and go in and change the ID to two it is going to give me an error message because this is the primary key which means that there cannot be the same ID for all the different users so in this case because I tried to change this one to two but we also do have another user that has an ID is two uh it it doesn’t allow me to do this uh so going back here I would have to go in and Tin this one to three because now I can tin this one to two because now there’s no duplicates okay so this right here is the right thing okay don’t go in and change these numbers to one and two because oh it makes sense to your brain uh to do this so I get this question quite a lot so I just wanted to make sure that this was answered properly in a lesson I do know I kind of went on a rant here because I do get this question quite often but I just wanted to make sure that people didn’t start changing the IDs manually inside a database because like I said it starts Breaking All the relationships to other tables and that kind of thing so uh with that said we have now learned how to insert update and delete data from inside our database tables so in the next video we’re going to talk a bit about how to select data from inside a database table since that is very important to know as so you can actually select and show data inside your websites and we will talk about selecting data but also how to join data together together if you have data from two different tables uh let’s actually go and do one last thing in preparation for that so let’s go ahead and copy the insert code that we have here go back inside our database make sure it’s selected and go inside our SQL console what I want to do is I want to insert inside comments and I want to insert a comment from one of the users inside my table here because that is going to be relevant when it comes to talking about joints and that kind of thing in the next episode so so what I will do is I’ll open up my comments inside a new tab so we can actually see what is going on and inside the comments table we do want to remember that we have a username a common text and also a users’s ID because these other ones the ID and the created app is going to be automatically created for us so we don’t need to worry about this one uh so we have these three columns here so username I’m just going to copy it go back in here and say I want to you know paste it in it was already in there so we didn’t need to paste anything I do also want to grab the common text paste that in as the second one and then I want to paste in the users’s ID now it is important to keep in mind that because the users uncore ID is a foreign key it has to match one of the users from inside my users table otherwise we’re going to get an error message because there’s no user connected to this comment here and we did set it up in the previous videos to make sure that we do need to have a user connected to it okay so it is important that we have a user that is existing inside the database so with the values here I’m just going to go and say we have a username so the username for this user could for example be cing and in this case here if we were to go back inside my database inside the users table uh cing right now has an ideas three so I’m just going to copy that then we’re going to go back in and make sure that the users ID which is the last one down here does have the same ID as that user inside the users table and I do also want to go in and write a message which is going to be my commment so I can say this is a comment on a website just so we have something in here and with this if I were to just copy this to make sure that we have it saved if I go down and click go you can now see that we’re going to have a comment inside my comments table so if we want to go in here you can see we have an ID we have a you know cing this is a comment on a website created that and then we do also have our users ID from the user inside our users table so now everything is working like intended so with this comment here we’re now ready for the next video where we’re going to select data from inside our database so with that said I hope you enjoyed and I’ll see you guys in the next [Music] video so in the last video I showed you how to insert update and delete data from inside a table and in this video we’re going to talk about how to select data since we we need to know how to do that in order to select data and show it inside a website so with that said let’s just go ahead and dive straight into it here uh in the last video we did create a couple of data inside our tables here so inside my comments table you can see that I have uh one comment from one user and inside my users table I do have two different users that I could do something with inside my database so we have Bessa and crossing and what I want to do here is I want to go inside my database and go back inside my SQL tab now again it looks a bit weird on my screen here cuz I’m zoomed in but if it were to zoom out you can see it’s right there um and what I want to do here is I want to create a select statement so we can actually select some data from inside one of the tables and actually show and we can do that directly inside our console here so if were to go in I can write a select statement so I could say I want to select and then I want to say what do I want to select you know what kind of columns do I want to select here so I could for example say username I could also write a comma and say I want to show the email and then I need to tell from which table inside our database they want to do this from so from inside our users table and then I want to write where so we can actually select these specific row that I want to select data from since otherwise it would just select all the users from inside the database um so right now what I want to do here is just kind of tell it which row they want to select from so I could for example say where the ID is equal to three in this case here so if I were to do this we can actually output data um so if you want to run this you can go down click go and then you can see we get some data in here so one total we got from this Cory here so if we want to scroll down you can see okay so we got a person called cing with an email called John Doe because he is the one that has a ID set to three inside my users table so we selected some data from inside this table here using the select statement and we got the information that we asked for which was the username and the email in this case here and we can do the same thing when it comes to a comment so what I can also do is I can go back up here make sure that we go inside our database I want to select the SQL console and that I can just paste in our select statements and say that I want to select the username the commentor text from not the users table but the comment table and we do want to make sure we change this last ID over here because if I were to go inside my comments table if you followed the last video uh you’ll know that we right now have an ID column but we do also have a users uncore ID and the ID column is the ID for this specific comment but the users uncore ID is the ID for the user who made this comment here so it’s very important that we we pick the right one here uh so users uncore ID is what we need to select over here so users uncore ID and I’m just going to go ahead and copy everything to make sure it’s saved go back down and then I’ll go and quy this inside my database base and then you can see we get another user or at least a comment from a user here called cing and we get this is a comment on a website now we do also have something else we can do which is something that you usually see people do when it comes to selecting data from a database if we were to go back inside my SQL console I can do the same thing but instead of writing which specific columns I want to pick something from I can go ahead and write a star symbol or a multiplication symbol um and that will actually go select everything from inside this row here so not just specific columns but everything okay so what I can do is I can just go and save this go back down run this query and then you can see we get everything so we get the ID the username the comment text the Creator ad and the users uncore ID of this particular comment here so it’s not really that difficult to select data from inside one of these tables you just need to run a select statement and just fill in the blanks essentially but now let’s talk about something something called a join which is when you select data from two different tables at the same time or actually you can do as many tables as you want but let’s just go and focus on two here uh so let’s say I want to select my user called clossing but I also want to select his comment from inside the comment table so now we’re actually grabbing two different uh rows of data from two different tables what we can do is I can go and make sure I select my database and open up the SQL console and I’m just going to go and paste in what we had here from the previous example so select everything from comments where users uncore ID is equal to three so now when it comes to creating a join we have a couple of different types that we can use we have something called a inner join we have something called a left join a right join and in some databases we do also have something called a full join but in my SQL databases like the one we’re using right here I do believe we do not have a full join so instead what you could do is create something called a left join and a right join Union where you basically combine those other two types of join to get the same effect as a full join um again I’m just kind of like rambling here and you probably don’t understand what I’m saying so let’s go and do a example here uh so what I want to do here is I want to go and create a inner join and in order to do that I’m going to say I want to select everything from users and I’m not going to go and add where ID is equal to three because that will actually be created a little bit later on inside this join uh so instead I want to go ah and say I want to select everything from users and do a inner join together with the other table that we have called comments and I want to make sure we do it on a certain column so right now we have two different columns from inside each table and those columns are going to have the same data inside of them to kind of connect them together so right now we have inside our users table we have an ID so our user has an ID is three and inside our comment table we have a comment where the users uncore ID is equal to three so what I want to do here is I want to combine these two tables where we have this same column data right cuz we made a foreign key so we can do that using the foreign key cuz that is perfect for that kind of thing so what I’ll do is I’ll go back inside my quy and say I want to select from my users table so users Dot and then you can see we get some options here so I’m going to select the ID where it’s equal to our comments Dot and then you can see we get users uncore ID and then basically spit out the data based on these two tables here so again before we quy this just to explain it again CU I know joints can be a little bit confusing to people uh we’re basically combining two tables together and we want to select everything from both of these tables and we start by selecting one table so in this case here the users table and then I want to injoin it with my comments table and I want to combine a data from these two tables using one column from each table that has the same data inside of it so if I were to C this just to make sure if we get any sort of Errors we still have it here if we were to scroll down and run this you can actually see that we’re going to get one result and that is going to be my user so in this case here with an ids3 uh cusing password email created at and then we also get the commment over here so everything is combined in one row of data and just to show another example here because you can also go back in here if I were to select my database and run another query and P paste everything back in you don’t have to select everything from both of these tables here you could also just go in and say that you want to select from the users table so users do uh username and you might also want to select something from the comments table so we can say comment dot comment text in this example here and maybe also another piece of data from the comments table so we can say comments. uh created at so if you want to select specific data you can also do that so we would actually run this scroll down you can see we get the same kind of thing but now we just get the The Columns that we asked for from both of these tables here and I do want to point out you that if I were to have another comment inside my comment table made by the other user named Bessa in that case it would actually get two rows of data down here so right now we we have this row of data here but we would actually get a second row below it with Bessa instead because all we’re asking is to grab data from two tables that has matching ID and user ID and spit it out and any other sort of data that does not have any matches is not going to get shown when we actually Cory this which is why we only get one uh piece of data down here but let’s go and talk about left and right join because that is going to change things a little bit so if we would going select my database go back inside my quy and just paste everything in and instead I’m going to do a left join which means that basically this users table is the primary table we have in focus and I want to join it together with any sort of comments that are made by my users so even if a user does not have a comment we’re still going to show all the users from inside our users table but they’re just not going to have the comments shown next to it so by doing a left join you can kind of see here that we have the users on the left here and we have comments on the right side and that is basically what we’re talking about here when we’re doing a left join so you know this is the left side this is the right side uh we can also do a right join which means that we’re talking about the comments on the right side and not the users on the left side so this is the the primary one that we’re focusing on over here but just to kind of demonstrate this if I were to do a left join I am now selecting all the users no matter if they have a comment assigned to them so in this case here if we were to go ahead and do this and run it inside below here you can see if I were to click go we would now get two pieces of data because I’m grabbing all the users but you’ll notice that the user that does not have a comment does not have any sort of data it just has null instead because we have no data so in this case you can kind of see that we still grab everything from our users table but not everything from our comments table only the comments that have a matching user is going to get grabbed and we can also do the same thing the other way so if were to go back inside my database and do a right join instead I can just paste everything in here and say I want to do a right join then I’m going to show all the comments but only the users that actually made a comment where these actually match when it comes to the column so in this case we just going to get one row of data which is going to be the exact same thing as when we did our inner join that is just coincidence by the way so would actually run this you can see we get one row of data and that is going to be our cing because he actually has a commment so we’re showing all the comments here but not all the users which in this case it would just be one row of data so if I were to have more than one comment inside my comments table uh we would have all those comments listed below here but the user information would just say null instead uh so the opposite of doing a left joint essentially in our case here with this example I have uh to show you and that is basically how to select data from inside our database and I just want to point out here that I know we talked about joints in this episode here in a lot of cases when it comes to just grabbing data from a database you’re just going to be doing what we did at the beginning of this video here you’re not going to be using joint for something specific that is more for other specific purposes inside your website we actually do want to combine data from different tables but in most cases you just going to be selecting data using what we did at the beginning so uh don’t worry too much about these joints here they are good to know uh but we’re not going to be using them you know when we start selecting data from inside our website in the upcoming examples when we actually uh start talking about how to use PHP to select data from inside a website database so with that said this is how you select data I hope you enjoyed and I’ll see you guys in the next video [Music] so in the last couple episodes we talked about how to go inside a database and create a table as well as creating data inside those tables and updating them and deleting them you know just kind of like manipulating data inside a database but we haven’t actually talked about how to do that from inside a website so that’s what’re going to do today as you can see I have a very basic website in front of me there’s not really anything special here just basically have a index of PHP which doesn’t really have anything there’s nothing inside the body tag um so what I want to do in here is I want to go ahead and actually create a connection to my database so I can actually go in and run PHP code that can actually query this database so in order to do that we need to have a connection going first so what I’m going to do is I’m going to go inside my project file and I’m going to create a new folder and I’m going to call this one includes now includes B basically means that this is going to contain files that are not going to be seen inside the actual website so for example a pure phsp file that just needs to run a script in order to do something but it’s not a actual file that we visibly see as a page you know in the same senses we see this uh index. PSP file because that’s the front page so the include files are just basically extra files that just run code the first thing I’m going to do inside this folder here is create a new file so I’m going to say I want to create a new file I’m going to call this one on dbh do in.php which stands for database handler. includes. PHP now it’s very important to point out here that it is possible to go in and name it as db. in which is also a kind of file that we can use and create phsp code inside of um but this kind of file can create issues and some people when they hear that I I call it in.php think that we are creating a in file that’s not what we’re doing here the in is just a naming convention so to speak so it doesn’t really do anything uh this is going to be a PHP file so in this case if we could call it db. in or you could call it db- Inc or just dbh Inc if you wanted to it’s really the same thing cuz it’s just a name uh so don’t get confused about the naming convention that I’m using here it’s just a way for us as the developer to know exactly what kind of file this is so what I’m going to do is I’m going to name this file and create it and inside this file I’m going to open up my PHP tag so we can actually create some PHP code and it’s important to point out here cuz we talked about this in my syntax video at the very beginning of this course that when we have a pure phsp file we don’t create a closing tag so for example you would create this at the end of a you know a pair of PHP tags we’re not going to do that when it’s a pure PHP file and the reason for that is if we were to accidentally go below here and create some HTML or something just by mistake then we can create more damage than you know not doing that again you can always go back and watch that episode at the beginning of the course if you want to know more about this but we’re just not going to put it in here okay um so going in here what we can do is we can start off by saying that we want to include some information about our database uh we did of course create a database in the past couple episodes so if I were to go in here you can see that I created a database called my first database and inside this database we also created a couple of tables that could actually have some sort of information inside of it so we have a comments table and we have a users table where we actually do have some users uh just because we learned how to insert and you know update and delete data and that kind of thing so we have some stuff in here is what I’m trying to say all we have to know for now is that we have a database called my first database because we do have this uh PHP my admin hooked up to our server so when we do actually need to connect to our database we do need to tell it which one of all these databases we’re trying to connect to because it is possible to use more than just one database inside a website you can use multiple if you want to and there there is Arguments for doing that you know for different reasons but for most websites you’re just going to have one database for everything um so remember the name my first database so going back inside our file here and I’m going to create a DSN which stands for data source name which is US telling our server uh what kind of database drial we’re trying to use and you know what is the dat database name uh what is the host that we’re trying to connect to here in this case it’s going to be Local Host so we need to give it a little bit of information about this database we’re trying to connect to uh so in our case we’re using a mySQL database so we’re going to create a variable I’m going to call this one DSN and I’m going to set it equal to a string and inside of this string here I’m going to say that I want to connect to a mySQL database driver then I want to tell it what kind of host I’m trying to connect to here in this case it’s going to be Loc host so we’re going to say equal to Local Host semicolon and then I want to tell it the database name which in this case here we did already just go in and check in my case it’s called my first database and again if you called it something else in the past couple episodes you do need to change this so it matches whatever you call your database um so you basically just go in and change the information here depending on what database you’re trying to connect to um so underneath here I need to give it two more pieces of information I want to give it a database username so we’re going to say DB username and this one is going to be equal to root and I do want to explain this in just a second but let’s go ahead and create the next one as well so I’m going to create a database password and this one is going to be empty in my case here password there we go um so basically when we have a database we do also have a username and a password in order to connect to our database which makes sense uh so in my case because I’m using my examp software and I have not gone in and actually changed this the default username and password is going to be root and then no password I do want to point something out here though because if you’re using a Mac computer you may need to go inside your password and write root um because I did experience 12 years ago when I was studying my web development bachelor’s degree that people who were using Mac computers and using xamp did actually need to include root in both places because XM is a little bit different on Mac than it is on Windows when it comes to at least this information here uh so if you’re sitting on Mac and doing this right here doesn’t work for you try writing root both places and see if that works for you or just Google how to change your password and username for your database again there’s a couple of different options here uh we’re just going to stick with this information for now and then I actually want to go down and run a TR catch block so we can actually see we get some a popup here and it looks like this so basically we have a TR cats block which means that we are basically running a blocker code and if an error occurs then I can do something else by catching the error and then doing something with the error mthod that’s basically what a TR catch is and you’ll see this very often inside PHP because a TR catch blog is very useful so what I’m going to do inside my try is I’m going to say that I want to run a PDO connection and we didn’t actually talk about this yet because when it comes to connecting to databases we have three different ways we can do it we have what is called a MySQL connection which is very bad and you should never use that because it’s obsolete and they actually came up with a new way to connect to a database which is called mysqli which stands for improved um this basically goes in and does extra SQL injection prevention um so don’t use my SQL because there is some security things that is just not very good but now let’s not talk more about was you cannot connect to a database cuz we have talked about that now but what you can do is you can connect to a database using mysqli I or we can also use the third method which is called PDO now PDO stands for PHP data objects which is basically another way for us to connect to a database that is a little bit more flexible when it comes to different types of databases out there mysqli is very good when it comes to mySQL databases but if you plan to connect to other types databases for example SQL light or something then you can use something like PDO it has also been a thing in the past lessons of mine that people do request that I use PDO so we are just going to stick to using PDO since that is going to be the one that people lean more towards because it is more flexible um but for people are curious about what exactly the difference is when it comes to the actual programming when it comes to mysqli and PDO um it’s basically just the methods that that you know change a little bit when you when you start programming it if you are curious about mysqli you’re more than welcome to look it up but we’re going to be using PDO in these lessons here so having ranted a little bit about different ways to connect to a database we are now going to create a PDO connection so PHP data objects is a way for us to create a database object when we want to connect to a database so basically we turn the connection into a object that we can use inside our phsp code and just refer to that object whenever we want to connect to a database so what we’re to do is we are going to have a variable called PDO I’m going to go inside of it and create a new PDO object so we’re going to say new PDO and what this basically does is that it instantiates a PDO object off of a existing class inside our PHP language that is going to create this connection based on a couple of parameters so for example what is the you know the database driver going to be what is the uh host going to be what is the database name we’re connecting to what is the username what is the password and then it’s going to create a database connection object that we can use so going inside this PD I’m going to give it a couple of parameters the first one is going to be the DSN which we just created up here uh so we have all of this information then I’m going to give it the username so we’re going to say DB username and then I’m going to give it the database password so doing this here we now have a database object and just to mention it here we could Technic technically just take this one lineer code and do this right here and that would actually be enough to connect to our database if all the information is correct every single time um but we do want to have some sort of error handlers you know if an error happens then we want to be able to grab that error message and show it inside our website um so you know even though this is like the pure bare bone you know enough to connect to a database uh it is a good idea to run this TR cats block here to you know get any sort of potential errors so what I want to do is I want to set a attributes inside this object that we created here uh we can do that by going in and say we want to grab this PDO variable that we just or object that we just created here because now it’s no longer a variable it is actually a object and I want to point to a existing method inside this object called set attributes which is going to allow for us to change a couple of attributes about this particular object that we just created for example how do we want to handle error messages that we may run into when we try to connect to our database so inside the parameters here I can say I want to grab a specific uh attribute so in this case it’s going to be PDO colon colon aore error mode so e r r m o d e you can actually see it pops up over here uh so we’re going to grab the arror mode and then we’re going to say we want to set it to a PDO colon colon e r r m o d eore exception so right now we are saying that if we get a error then we want to throw a exception which means that we can go down inside our cats block down here and actually grab that exception which is you know information about this error here so what I can do is I can say I want to catch my PDO exception and I want to name this one variable e so we’re basically just saying that this is a PD exception type which is going to be named as variable e which is a placeholder that we can refer to inside the curly brackets here so if an error message happens then I want to go in and Echo out a message connection failed colon space and then I want to concatenate a error message so right now we are grabing the Exception by referring to variable e so I want to go after here and paste that in and say I want to run a method called get message parenthesis and semicolon so right now we’re getting the exception which is the error that maybe thrown and then we want to grab the actual error message and Echo that out inside the browser so if we don’t connect correctly to a database then just go ahead and throw an exception here but like I said this one liner code here is actually the one line that we use in order to actually connect to our database so all this other stuff down here is error handling and throwing you know error messages inside the screen if the connection fails that kind of thing so for now just know that this line here is the important one so now that we have this we can actually go in and actually do stuff inside our code so if I wanted to you know select data from a database or if I want to insert data inside my database I can do that by simply running uh this one connection here and actually querry something into my database using PHP code and we haven’t talked about how to do that yet but that is something we’re going to talk about in the next upcoming lessons uh so for now we learn how to connect to our database and in the next upcoming lessons we’re going to learn how to insert data we’re going to learn how to update and delete data so we can actually use the connection for something so I hope you enjoyed this lesson and I’ll see you guys next [Music] time now I just got done watching the last video that I uploaded to my channel and I thought why not just show you how to go in and change your username and password when it comes to connecting to a database because if some of you are sitting there with with you know a Mac computer or something where things are a little bit different when it comes to the password then you may want to know how to change your username and password in order to you know decide yourself what you want it to be and as I did that I did also run into a very well-known bug when it comes to PHP my admin that actually prevents you from going in and clicking on user accounts in order to change your username and password now the error you might be getting is called exam error number 1034 index for table DB is corrupt or something like that so you know having something corrupt inside a table uh there is a very easy way to solve it so I will show you how to do that after I’ll show you how to go and change your username and password and then you know at the end of the video I’ll show you how to solve a corrupt table so the way you change your username and password is by going inside PHP my admin by clicking it up here to make sure you’re inside the main page of PHP my admin uh because if you click a database you can see all the menus change so that is not what you want to do so going inside PHP my admin we’re going to go under user accounts and then you can see we have all these different users that are here as a default now the one that you’re looking for is the one that has a host name of Local Host and a username as root how do we know that well we used Root in the last episode where we actually connected to our database so you know you need to have one where the username is root and also where we connect to a host called Local Host so this is going to be the one down here at the bottom so what you can do is click on edit privileges so you can go in here and then you can see we get a new menu where we can change this user here uh we do have one up here called login information so if you click that one you can see that we can change the username from root to something that we may want it to be um so if I want this to be Denny or something else then we can change it in here just change it go down and actually you know click go at the bottom here in order to make the changes happen uh do not go in here and do the password in here cuz there is apparently a bug that can happen where you go in and change the password directly in here uh I’m not saying it is going to happen to you but it is better to go back up inside the top here where it says change password click it and then change it in here instead so you go in here uh you choose to have a password then you enter you know something so in this case it could be anything that you might want to think of and then you retype it and then you click go and then you basically change the user information and the password for the user that we use in order to connect to a database um but now let’s talk about the bug that you may encounter which is the one called eror code 1034 uh so basically when you click this user accounts up here instead of going inside right here it may throw a error which gives you a popup with the error message that I just told you about and basically can’t access this page here the way you’re going to fix it is by first of all figure out where exactly the corruption is because that is what you need to find out in order to solve it uh so the way you can do that is by going back inside PHP my admin and then clicking on whatever database that it’s actually telling you there is something corrupt inside of so when you give the error message it’s going to give you a quy string where it is going to say there is a corruption or an error happening from a certain database uh so it’s going to have a qu that says something something from a certain database which in my case was from the mySQL database which is the most common one to get this corruption in so if you were to go down click the mySQL database over here in the side you’re going to see all the tables from in here uh in my case it did actually tell me me inside the error message that it was from inside the database table but if you’re a little bit doubt about where the croping is you can scroll down to the bottom here and go down to where you can check all and then with selected you’re going to say you want to check table and when you do that it is going to check and give you you know some status thing now in my case just checking the tables here automatically fixed the error for me which I actually thought I was going to show you how to actually fix the error when I started this video here because I did not go in and repaired it yet uh but just checking the table gave me a couple of error messages it said that my MySQL DB here there was some sort of Errors inside of it it was corrupt uh it gave me red warning messages and I basically just went back again to PHP my admin from here and that fixed the errors for me uh but if that doesn’t work for you then you want to go down and just take note that it was the MySQL do DB table go back inside the mySQL database and then you want to make sure you select the DB table that is over here then you’re going to scroll down to the bottom and with selected you’re going to say repair table and when you do that it is going to try and repair it and when you do that it should also fix the error message for you so after you repaired the table that is broken uh you can go back inside PHP my admin and then you can click on user accounts and then everything should be fixed so you can go in here and and actually um you know change your username and password so that is how I can solve it in my case it solved it just by me checking the table I didn’t even have to repair it in order to to fix fix this issue here but if checking it doesn’t work then choose repair and basically that should work for you so with that said I hope you enjoyed this lesson lesson fix I guess this is kind of well I did show you how to change the username password so this could be considered a lesson so um thank you for watching and I’ll see you guys next time [Music] so in the last episode we learned how to connect to a database directly from inside a website and in this episode we’re going to learn how to insert data directly from inside our website so we don’t have to go inside the database and start typing SQL code in order to do that uh so we’re going to do everything from inside our website here yes I did cut my hair uh it is quite a bit shorter for health reasons so it is a little bit different it wasn’t my choice but it is what it is I do want to start out by pointing out a little bit about what exactly we’re going to be doing when we connect to our database and insert data into our database since there is a couple of ways you can do it uh we did talk about us using PDO in the last episode which is what we’re going to stick to in this video here so we’re not going to use mysqli or MySQL which is outdated uh we will be continuing to use PDO to do this and we’re also going to be using something called prepared statements and that is something that is very important for you to do uh you can insert data into a database without using prepared statements but that is not secure so I don’t think it’s a good idea to teach you how to do it without using prepared statements since there’s never a reason for you to do so uh so we will be using prepared statements in order to securely insert data into a database and just to talk a bit about what exactly prepared statements are and what exactly they’re supposed to do uh let’s say we have a website like this where we have a signup form where you can go ahead and type in your username your password your email and if you use a were to go inside your website here and go inside one of these puts it is actually possible to type code directly inside these inputs here so just like we talked about cross site scripting in a previous episode like you had to sanitize your data and validate it to make sure that people couldn’t inject JavaScript code into your website it is also possible to go in and write SQL code so if you were to write SQL code directly inside this input and the user submits it then they can actually destroy your database because maybe they decide to write a SQL query that can go in just delete the database or something so to prevent the user from being able to write SQL code directly inside an input like we have here uh we need to use prepared statements now the way a prepared statement work is basically we send in the query that we write so the SQL code and we send that to the database first and then afterwards we bind data submitted by the user and then send that to the database afterwards so because we separate the query from the data that the user submits to us we can do them separate ly and not have SQL code have an impact on the query that we write inside our PHP code because they’re separated so using prepared statements is a very good idea so having talked a bit about that let’s actually get started on creating an actual PHP script that can actually insert data into our website so going back inside our editor here you’ll notice that I do have one thing that you do not have from the previous episode uh so in the last episode we did create this database Handler together uh where we can just go in and grab this PDO variable in order to connect to our database but inside my index page I do actually have a form that I created which is the one you just saw inside the browser uh this is just basic HTML form you should know HTML by now so this shouldn’t be anything new to you uh this is just a basic form where I go in and say I want to submit this data to a PHP file which is going to be inside by includes folder called form handler. in.php we did talk about the naming convention that I use here with Inc so if you watched the last episode I did explain that in that episode and I am using a post method since we need to submit data and when we submit data it is more secure to use a post method and that is the method you’re going to be using most of the time when it comes to submitting user data when you want to grab data from a database you use a get method most of the time and then you can see I have a couple of inputs down here I have one for the username I have one for the password and I do also have one for the email address now I do want to point something something out here which is that we do have an attribute inside each of these form inputs which is called name uh we did talk about this in a previous episode I we talked about submitting data using a form uh this name attribute is the name that we’re going to be grabbing inside this file up here when we send the data to the other page so it is very important that you have a name attribute and you remember what they are or you can just go back and and look at your form um so you know exactly what you need to grab in order to grab the data so having talked about this uh let’s go ahead and start creating this form handler. in.php file since that is what we need in order to actually you know run this data submitted by the user so we can actually insert it inside our database I do also want to point out to you before we continue that this is the data that is fitting into the table that we created together in the uh table episode so as you can remember we did actually go inside our database here and we created two tables we created a comment and a user table and inside the users table we do have an ID username password email and created ad now we did set it up so that ID and created ad is automatically created for us so we don’t need to submit any sort of data for that uh but we do need to submit data for username password and email which just so happens to be the three inputs that I included inside my form so now that we know this let’s actually go and create our form handler. in. phsp file so I’m going to go inside my includes folder over here right click say I want to create a new file I’m going to call it for form handler. in.php and having created this one we can now start creating a script that actually goes in and submits this data to our database so the first thing we’re going to be doing is we’re going to start up our PHP tags we’re not going to close them though which we talked about in the previous episode since this is a pure phsp file that is just going to run a script and then that’s it we don’t need to have a closing tag because it can actually cause issues which we don’t want to happen the first thing we’re going to do is to actually run a check to see if the User submitted the data and entered this page the correct way because it is actually possible to go inside our website here and go inside the URL and then directly say I want to go inside my includes folder forward slash and then form Handler uh. in.php and then you can see I actually enter this script that we just created and entering this page here in the way that we just did just by typing into the URL is not a good thing uh so we do need to make should we check if the user actually submitted this form in order to access that page because otherwise we don’t want them to access it so going inside our code I’m going to go in and create a if statement I’m just basically going to check for a super Global so in this case we’re checking for a dollar signore server and then I want to set brackets and go inside and say I’m looking for a request method requestor method and check if it’s equal to a post method so if the user actually submitted a form using a post method which we did actually do cuz we just did it right here and enter this page using that method there if not then I want to create an lse statement and basically say that I want to send the user back to the front page because you know they’re not supposed to be here and we can do that using something called a header function so basically create a header function and say we want to add a location colon and then you add in the link that you want to send them to so in this case here we’re inside a includes folder so in order to get to our index page we have to go back One Directory so we say do do forward slash and then we say index. PHP so basically now if the user tries to access this page without actually submitting the form so if we were to go inside the URL here and say I want to go inside my includes folder and access this page you can see oh okay now I got sent back to the front page so everything is working perfectly so now what we want to do is we want to go inside the actual if condition and say okay so if we did actually access this page legitimately then I want to actually grab the user data so I’m going to create a variable called username and I’m going to set this one equal to a dollar signore post since we sent this data using a post method and inside of here I’m going to reference to the name attribute that we actually submitted inside uh the form so in this case here we call the username or at least I did I don’t know what you called it but if you followed my tutorial you did call a username and then I’m going to cover this down two more times and the second one is going to be PWD for password and the third one is going to be email and just like so we now grab the data and you may point something out here because hey Daniel you forgot something you didn’t use the HTML special characters function in order to sanitize the data why didn’t you do that uh this is actually something we have to do when we want to actually output data inside the browser so when you’re not outputting data into the browser it is not dangerous at least as it is right now to not sanitize the data so anytime you have to Output data into the browser and actually spit it out so if we to go down here and actually uh do something like this here so if I go down a couple of lines and say I want to Echo out the username then I would need to sanitize this because I’m now outputting data into the browser so I would need to go in and actually wrap this in HTML special characters otherwise this is not going to work and it’s going to be unsecure but because right now we’re just submitting data into a database and not outputting it inside the browser uh we’re not going to be sanitizing anything just quite yet you can of course do it if you want to and sanitize the data like we did in the last couple of episodes and just you know submit the data into the database being sanitized uh but it is best practice not to do so unless you actually try to Output data into the browser and the reason for that is that we are converting this to HTML special characters so in some cases you know we want to use data from inside a database uh we don’t necessarily want to have it in HTML special characters and use it inside our code for example if we’re not planning to actually output it inside the browser so do be aware that there are some cases where you don’t want to have HTML special characters translated data inside your database so in some cases you don’t want to have it the next thing I’m going to do here is I’m going to run a TR cats block which we talked about in the last episode basically we’re just trying to run a blocker code and if it fails then we want to catch an exception so we’re just going to go down here and say if there is some sort of error happening then I do want to go in and say I want to grab a PDO exception and I’m going to create a variable e as a placeholder that I can refer to and then inside of here if something happens that goes wrong when I actually try to insert this data into the website then I do want to you know output a error method so I’m going to die which is a function we have inside PHP that is just basically going to terminate this entire script and stop it from running and it’s going to Output a error message so going in here we can actually say we want to write a custom error message in this case here I could say something like Cory failed so I’m going to say Cory failed colon space and then I can concatenate the error message so I’m going to point to the exception and then get method on up method message get message it is a method but it is called get message so it is the same thing um the next thing I’m going to do is I’m going to go inside this try block that we have up here and I’m actually going to grab my connection to our database because we have that inside our db. in.php file to do that I’m going to use something called require require uncore once and this is basically going to say that we want to link to a file that we have somewhere so I’m going to grab a PHP file for example and just say we want to link it inside the script here so when I go in and say I want to link to a db. in.php file I’m just basically linking to this file that we have up here do keep in mind that because I’m inside the includes folder right now and typing this script here uh we don’t need to go inside another directory or something if this dbh the link the PHP was inside another directory you would of course need to go back out of the directory and go inside the correct directory and doing this here is basically the same thing and just going in and saying oh okay I’m just going to include all this code and just paste it in here like this is the exact same thing so we’re just basically linking to another file which means that we have access to all the code inside that file uh after this point here and I do also want to point out because I don’t think we talked about this yet we do have require uncore once we do also have require if I can spell that correctly there we go we do also have something called include so we can say include and we can also say include underscore ones all of these basically do the same thing but with slight variations so include for example we’ll go in and say oh okay so we’re going to include this file just like we did up here but if we can’t find the file then it’s going to give you a small warning saying oh I can’t find the file include underscore once it’s going to do the same thing but it’s also going to check if the file has already been included earlier inside the script and if it has then it’s going to throw you a warning and when it comes to require and require underscore once they do the same thing as include and include underscore ones except instead of just throwing a warning it is going to actually run a error so going to have a fatal error saying oh okay we can’t find this file so stop everything from running or it’s going to say oh you already included this file once so stop everything from running uh so these two slight variations of each other with you know different exceptions in this case here we’re just going to go and use require uncore once because we don’t want to run the connection if we already have the connection included somewhere else so what I’m going to do now is I’m going to write a variable called query because I now want to actually create a query that I can send inside the database to insert data so I’m going to set this one equal to a string which is going to be our SQL quy string that we’re going to submit and I’m going to run a insert statement and you may recognize this one because we did learn how to do this inside our database episode this is the exact same thing so the SQL code is basically insert into and then we’re going to choose a table so in this case it’s users and I also want to make sure that we include our column names so in this case if we have username we do also have some something called a PWD and then we have email then I want to include the values so I’m going to say values and then parentheses and I’m just going to go and wrap my code here to make sure that it doesn’t disappear off screen so it goes down to next line instead and then I’m going to go inside and give it the actual values now we could do this here and just say we want to copy the variable and just paste it in and say comma space and then password paste it in and then the email and paste it in and this would actually be okay uh do keep in mind to close off with the semicolon at the end here because this is a SQL statement which means that you do need to end off the SQL statement with a semicolon just like we did inside the database episode so it may look a little bit weird that we have a semicolon here and also one here but do keep in mind this is the SQL and this is the PHP but like I said earlier we’re not supposed to insert user data directly inside our query otherwise they can do something called SQL injection and destroy our data base so doing it like this is not really seen as a good practice now there is two ways we can use a prepared statement either you can use something called name parameters or you can not use name parameters I will show you how to do both ways and we’re just going to do one of them at a time so using not name parameters what you basically just do is you replace these different user data with question marks so you say question mark question mark and question mark and these are going to act as placeholders so we later on can actually go in and insert this data or bind the user data to this query after we submitted the query so going down to next line I’m going to create an actual statement which is a prepared statement that I can actually prepare to query this query inside the database so what I’ll do here is create a variable called stmt for statement then I’m going to set it equal to our database connection which is variable PDO which we have access to now because we actually required this file up here and then I’ll point to a method called prepare parenthesis semicolon and then inside this prepare statement I’m going to submit my query so basically now I’m submitting the quy to the database so it gets run into the database and then afterwards I can actually go and say okay but now I’m going to give you the data that the User submitted so I’m going to reference to the statement we just created so statement and I’ll go ahead and point to another method called execute so parenthesis and semicolon and inside this method here I’m just basically going to submit the user data and I’m going to do that using a array so I’m going to add a pair of brackets and then I’m going to go in and just submit these data one by one so we’re going to say username then we’re going to say password and then we’re going to say email so doing this here is going to actually submit the data from the user and actually sign them up inside the website uh but before we test this out let’s actually go ahead and just finish off the script here uh cuz there’s a couple more things that we need to have in in order for this to actually be kind of proper properly done the first thing we’re going to do is manually close the statement and also the connection to our database it’s not something you have to do because this is actually going to happen automatically uh but it is considered best practice to do so manually to free up resources as early on as you can uh so going down here what I’ll do is I’ll refer to my uh database connection so that is variable PDO and I’m going to set it equal to null then I’m going to go ahead and go in and say I want to grab my statement and I’m going to set it equal to null and I just want to point out here there’s a couple of ways you can do this there’s also methods for closing off a connection or a statement um but I’m just going to refer them to null which is the same thing as just saying okay so just you know not set them equal to anything and free up those resources and the last thing I’m going to do is I’m going to write a die method just like we did below here when you know some sort of error message happens then we want everything to stop running I do also want to point out here that you can use dive or you can use something called exit and people do argue a lot about you know whether or not it doesn’t matter which one you’re using the general rule of thumb is that if you’re just closing off a script that doesn’t have any sort of connection running then just use exit but if you’re running something that has a connection inside of it then use die and of course we do also need to make sure we send the user back to the front page after they signed up inside our website so I do want to go down here and copy this header function and then paste it in right above the die statement so we send the user back to the front page and then kill off this script here so this is everything that we need in order to get this working so I could actually go inside my website here and go in and say I want to sign up John uh do you know just to give him some sort of you know username uh password is going to be one 12 three and then I can call his email John gmail.com just to give him something if I were to sign him up inside the website you can see we get back to our front page if I go inside the database and refresh it you can now see that we have another person signed up inside our website so as you can see our script is working perfectly I do want to point something out here though which is something that I know some people might point out uh why is my user id 10 on this person here it’s just basically because I inserted some users before this tutorial here so the the ID is going to change a little bit compared to yours so having done this we now did this using non-name parameters but what about name parameters inside our code uh so if we were to go back into inside the form Handler I do recommend using name parameters because it actually allow for us to know inside this query here which data is supposed to be inside where when it comes to using non-name parameters like we did here I do also want to point out that the order in which you insert the data down here inside the execute has to be the same order as inside the columns up here so these have to match up with each other but when we use name parameters this is not the case because I can actually go in and say instead of question mark I’m going to write a colon and then then I can give it some kind of name so I can say something like username uh I can also say the second one is going to be colon PWD then I can say the third one is going to be colon and then email and in this sort of way instead of question marks I’m now giving them actual names so after preparing my query here I can go below and I can actually go ahead and bind my user data to these different name parameters up here and I can do that by referring to my statement so I’m going to say statement and then I’m going to point to a method called bind param which stands for parameters then I can go in here and say that I want to have two pieces of information I want to first of all have the actual name parameter so the first one is going to be the username going to insert that one and then the second one is going to be the actual user data so in this case our username variable up here so if we were to paste that in we now have a name parameter bound to a actual data submitted by the user so I’m going to copy this down two more times and I’m just simply going to change these so password then I’m going to change to email and then I want to make sure I delete the array that we have inside the execute down here because now we don’t need it anymore because we actually B them up here instead so doing it like this we now use name parameters instead of notame parameters so what I can do is I can go inside our website here and test this out one more time so I can say Jane do in this case here so we can say pass 1 2 3 4 then I can say Jan gmail.com and then I can sign up go inside the database refresh it and then you can see we have Jane do instead and this is basically how we can go in and actually submit data using our PHP code from a website instead of going directly inside a database and manually coring the database in there so this is how we can insert data uh I hope you enjoyed this episode and the next one we’re going to talk about how to actually update and delete data and then after that one we’re going to talk about how to select data and show it inside our website so hope you enjoyed and I’ll see you guys in the next video [Music] so in the last episode we learned how to insert data into our database directly from inside our website and this episode we’re going to talk about how to update and delete data from inside our database directly from inside our website now as you can see I did change things a little bit from the last episode inside my index page I did actually include a second form and I did also change a couple of things inside the original form up here uh so just to quickly go over what exactly I changed I did go in and change the title so now it says change account I did also include a title for the second form down here so it says the leete account and I went in and changed the action of the first one so went inside the update form and said I wanted to send all the user data to a user update page that we haven’t created yet but we’re going to in just a second and inside the delete form down here I just went in and said I wanted to send the data to a user delete. in the PHP file and I did also delete the email input from inside this form and that is basically all I did here and just to point it out here I do also have the form handler. in the PHP found in the last episode since we basically just need to copy paste everything so just to show it I still have it in here so we have the code here uh for people who have not followed the last episode you can just kind of copy paste what I have in here um and just use this code when it comes to that next part so what I’m going to do is I’m going to go inside my include folder and create these files here so I’m going to create a user update. in phsp so I’m going to right click on includes and say I want to include a new file and just basically paste in the name of that file then I’m also going to be creating one for the delete user or user delete. in.php so I’m going to copy the name here go inside includes right click and create a new file so the first thing we’re going to do is of course talk about how to update a user inside our website so what I’ll do is I’ll go inside my user update and paste in the code from my form Handler so going inside my form Handler I’m just going to copy everything go inside user update and just paste everything in and all you need to do here is essentially just go in and say you want to change the query down there just slightly so it actually matches up with a update statement instead of a insert statement so it’s quite simple to just go in and run a update statement instead of a instant statement uh from the last episode so I’m just going to text wrap everything here so we can see everything on screen and we’re just basically going to change the insert statement into a update statement so we’re going to say we want to to go in here and update and I want to update my users table and I want to set some certain values I can just basically delete everything that we have here I want to set the username equal to something new which in this case is going to be a placeholder uh because we did talk about prepared statements in the last episode so we are going to create a placeholder called username and then afterwards here we’re going to say what else needs to be changed so in this case I do also have a password column that needs to be set equal to the password submitted by the user so again we’re going to say single quotes and inside of here we’re going to refer to a placeholder called PWD and then we’re going to add in the last one which is going to be the email so we have a email column that is going to be set equal to again single quotes and then we insert a placeholder called email after doing this we need to tell it where inside the table we want to change this because if I were to just submit this then all the users inside my table are going to be uh updated to what the User submitted just now so I want to go in and say where and in this case here we’re just going to go and say we want to grab a user that has a certain ID as something specific so I’m just going to say ID is equal go inside my database here and just pick a random user that I have so I can say Bess is going to have his username changed uh so we’re going to say his ID is two I’m going to go inside and again if you have another user with a different ID just go ahead and choose some sort of user from your database in my case I’m going to choose B that has an ID as2 um of course this is very unorthodox because typically inside a real website you would have a user that is locked into the website currently who’s trying to change his user information and because of that you would actually have his user ID grabbed and stored inside a session variable so we could actually just grab the user ID and say oh okay so that’s the user we need to change this information of uh so right now we’re just manually going inside the database and grabbing a random user and typing it in here cuz you know we’re not we don’t really have a real login system right now so this is just for practice okay so we’re just grabbing a random user here uh so doing this now basically what you would just do is you would go down you would bind the parameters in the same sense you know you would actually prepare the statement find the parameters execute the statement and that is actually pretty much it I just realized that we don’t actually need to have these single quotes up here so let’s actually go and delete those so we’re not going to have single quotes around the user data uh in inside this quy up here so I’m just going to go and delete them like so and with that Sav we’re going to go back inside the website and actually test this out so I’m going to actually have something written in so in my case here I’m going to change bass’s username to Bassa is cool and I’m going to have 1 2 3 4 as the password and then I’m going to change the email to bis cool at gmail.com and if I were to click update here you can now see that we get sent back to the front page if I go inside the database refresh it and now everything is has been updated so Bessa now has a username as B is cool 1234 and B iscool gmail.com so everything gets updated in here and looks correct but now what if I want to delete a user from inside my website how can we do that because that is also very simple to do uh so if I were to go inside my user delete and copy paste everything from inside my form Handler so we’re just going to copy everything again insert that inside user delete now we basically do the same thing we just go inside the Corey up here and say okay so we’re not running a insert statement we are actually running a delete statement and this one is going to be even easier because we have less data to handle so we can just go inside and say okay so in this case here the user did actually not submit a email so I’m just going to go and delete that one for now then I’m going to go down and change my insert into statement into a delete statement so I’m going to say I’m going to delete from users and I’m going to say where a certain username and password is equal to what the user submitted I could also use an ID just like we did with update cuz that would be the typical thing when you have an actual login system where the user is logged in so you have their ID and you can do that uh but for now let’s just go Ahad and use the username and password since we did submit it here so why not just use it so I’m going to say where the username column is going to be equal to a placeholder which is username and then I’m going to say and where the password column is going to be equal to the password that the User submitted and do also make sure you close off with a semicolon here and then what I’m going to do is I’m going to go down delete the last bind parameter because we don’t need that since we don’t have a email and this is basically all we have to do then we can actually go inside and delete the user so if I were to save this go inside my website here and refresh everything and say I now want to go in and delete the user that has a certain username and password so in this case I could say let’s go and delete Danny that has a password as 123 so we would to go inside my website here go down inside delete account I can say Danny that has a password as 1 2 3 delete his account then we’ll back again so if we were to go inside and refresh you can now see that Danny has been deleted and this is basically how you can go in and update and delete data from inside a database directly from inside a website so it’s quite simple to do uh in the next video we’re going to talk about how to actually select data and show it inside our website so that is going to be very fun to do so I hope you enjoyed this lesson and I’ll see you guys in the next video [Music] so now that we learn how to insert update and delete things from inside our database directly from inside the website now we have to talk about how to actually select things from inside a database and show it inside our website so to begin with let’s go and take a look at what exactly I have inside my text editor so you can actually you know copy what I have here since we need a little bit of HTML in order to get this working inside my index page here I do have a very basic form at the bottom I’m just going to go and text wrap so you can actually see what’s going on here I just have a very basic form that I styled a little bit inside my website so it actually looks somewhat nice to look at uh which is why I have this class up here you can style this in however way that you want to The Styling has nothing to do with the phsp so it’s still going to work even if it doesn’t look very good inside your website I just did it to sort of like have something nice for this tutorial here but as you’ll notice inside the form here I actually included a action that does take us inside a include file now we’re actually just going directly to a regular page inside the website here called search.php and we’re still using a post method since we’re trying to submit data in this case here inside the form you can see I have a very basic label just to have a label for this input down here that goes in and just simply takes a input from the user where we can search for something inside the database now in this case here because I wanted to create something just a a very simple little thing um I wanted to go inside database and search for any sort of comments made by a certain user where we type in the username of that user so if that user made a comment then I want all the comments to show inside that page that I’m linking to called search. PHP so technically we’re creating a small search system here for a website so if you want to have a search system inside a website you can build a very basic one in this sort of sense here uh so you actually learn how to build something that you could potentially use inside a small project if you wanted to so so essentially it’s just a basic form where we go in and we type in a search word and then we just search for something so uh the only important thing here to really note is where exactly we’re taking this data to so in this case the action the method as well as the input down here where we actually assigned a name called user search so with this basic form here let’s actually go and start creating a search. PHP file so going inside our project folder I’m just going to go and create a new file not inside the includes folder but just inside where I have my index page uh so I’m just going to create a search. PHP and then I’m just going to go ahead and create it now inside this file here I’m just going to copy paste everything from inside my index page since this is just a regular page inside our website so I’m just going to copy everything and paste it in and what I’m going to do here is I’m just going to go and remove my form since I don’t think we need to have it inside this page here and what we have to do now is just basically go in and write some PHP code that queries our database and searches for certain uh data in inside the database using the search term that we wanted to search for and show it inside this page here so what we’re going to do is we’re going to go to the very top of this file and I’m going to go above my doc type and I’m just going to start off my PHP tags now in this case here we do actually have HTML like this is an actual page inside our website so we do need to include a closing tag around the PHP code here so we can’t just not have it like we did in the previous episodes um so what we’re going to do is we’re actually going to go in inside our previous file just any of the previous files we created in one of the last episodes um so we did create a form Handler we created a user delete and a user update it doesn’t really matter which one you want to to go into I’m just going to use the form handler. ink the PHP file here and as you can see we have a bunch of PHP code that we did create in the last couple of episodes I’m just going to text wrap everything here so we can actually see everything if you want to copy what I have here you can just kind of see what I have and I can just very slowly here scroll past it and you can pause the video uh but this is something that we did create in the past couple of episodes um so what I’ll do is I’ll copy everything except for the beginning PHP tags since we don’t need that cuz I did open and close phsp tags inside the top of my search. phsp file here so I’m going to paste everything inside at the top of the site here and inside of this PHP code we now just need to make the alterations that we need in order to match it up with the form from inside the index page so if I were to go inside my index page again you can see that we did have this form that I talked about extensively a minute ago um and we just basically have one field which is called user search so I’m going to copy that name go back inside my search. phsp and say I want to grab a user search and I’m just going to delete the other two uh post methods so I’m just going to change the name here as well to user search so we do actually have a variable that makes sense with the naming and with this we can now just go down and change the Quarry since we need to make sure this is actually a select statement we have learned how to do that in a previous database episode uh so what I’m going to do is I’m just going to go and delete everything again we’re just going to make sure we text wrap here so I can select everything and I’m just going to delete the entire query that we have in here what I’ll do is I’ll create a select statement so I’ll say select uh everything so select all from a comments which is our comment table inside the database where a username is equal to a certain value in this case here it is going to be our user search so I’m just going to write a placeholder called user search and this is basically all we need inside this quy here we’re just selecting all the data from inside that row including you know the the actual comment the user made when the comment was made which user made that particular comment uh what user ID do they have what just selecting everything from inside that row inside our comment table just to show exactly what comments table I’m talking talking about here if I were to go back inside my database so inside my database here we do still have the same tables that we did create together some episodes ago I’m going to go and Link those episodes in the description if there’s someone following this tutorial here thinking oh well I don’t have this so I’ll go ahe and Link that in the description so you can actually keep up with what we’re doing here but basically we just created two tables we created one called users and one called comments and inside my comments table I actually went in and included a couple extra comments uh which we did learn how to do so you can go back again watch that episode and create some more comments in there I just basically went in and manually typed those in inside the SQL tab up here I created a couple of comments for cing a couple for bess’s cool and Jane do because those are some of the users that I have inside my website so if I were to go inside my users table you can see that I have some of these users in here so I do have quite a few comments now uh that I can go inside my website and I can actually pull these out depending on the search term that we put inside our query so going back in you can see we’re selecting everything from comments where a username is equal to what we submitted inside the website so going down below here uh first of all we need to actually send in the query so we’re doing that with this prepare statement here again it is important to mention here that we are going back to grab our database file which is inside a includes folder with the connection to our database again this is something we learned in a previous episode which I’ll also link below if you need to have that one but we do need to go in and actually change the path because like I said we’re not inside the includes folder anymore so inside the require above here where we actually grab this database connection we do need to go inside the path and say we want to go inside and includes folder and then grab the dbh that in the PHP file otherwise it’s going to say oh we can’t find this file cuz it’s not linked correctly right so with this done I’m going to go back down and the place where we actually bind the data that we submitted from the actual form uh we do need to change these as well since we don’t have three pieces of data data submitted we just have one and we do also need to change the names of these down here so I’m going to copy my user Search and say that is what I have as a placeholder and I’m also going to grab the data called user search and put that in as my variable and then I’m simply going to execute the statement here and at this point here things are going to be a little bit different than the previous couple of episodes so we just simply went and made changes to the database or submitted data to it uh because when it comes to actually grabbing data we need to actually grab the data and set equal to an array inside our code so we can actually do something with this data so we need to have the data put inside our PHP code so to speak so what I’m going to do is I’m going to go below my execute and I’m just going to go and create a variable called results and I want to say that I’m going to take my statement that we sent in and execute it and I want to point to a method called Fetch all because in this case I want to fetch all the results from the database there is also something just called Fetch for just one piece of data but in this case here we are potentially grabbing many comment from the database so this is going to be uh many results in our case so fetch all is the one that we’re going to be using parenthesis and semicolon and inside the parentheses we need to tell it how do we want to fetch this data now we did talk about arrays in the past because we did have a array episode in one of the previous ones and inside that episode we talked about indexed arrays and we also talked about associative arrays and when it comes to grabbing data from inside a database it is is much easier to handle the data as an associative array because essentially each array data is going to have a name associated to it so we can refer to the name in order to get the data and in this case when we grab database data the column names inside the database are going to be the name for each data so doing this as an associative array is a very easy and fun way for us to grab data from inside a database in order to know exactly what we’re referring to so what I’ll do is I’ll go inside and say I am using a PDO connection and I want to fetch this as an associative array so we’re going to say fetch ESO which is for associative so doing this here we can actually go down and actually delete this header statement because we’re not trying to get back to an index page or anything here uh we’re just basically going to say that we don’t you know we we have the data so now everything is done we don’t need to do anything else here I do think it’s also important to point out here that we do still have this header function down here since that if the user tries to access this search page without actually having searched I don’t see a reason for them to access this search the PHP page um so if they were to try and access this file without having actually searched for something there’s really no need for them to be in here so again just to summarize all the data that we just grabbed using the query from the database are now stored inside variable results as an array and each data can be referred to using a associative name so in this case if I want to grab um let’s go back inside our database here if I want to grab this particular comment down here from a certain user so let’s say I search for crossing and now I’m grabbing all the comments from Crossing if I want to grab this is a comment on a website then I just need to refer to commentor text because that is the column name and this is going to be what is assigned to our associative array as the name for this piece of data I think I may be confusing you more just by explaining this over and over again so let’s actually just go Ahad and do it CU that’s a lot easier for you to see um so what I’ll do is I’ll go down inside my body page and actually do something with this data because we grab the data at the top of our file so we can just go further down to file and just refer to variable results because it’s available inside this file here cuz we just created it uh so you can just go aead and go further down to file go in and say that maybe we want to create an H3 where we can say this is our search results go down below here open up the PHP tags so we’re going to open up and we’re also going to go and close it here and then we’re just simply going to do a simple if statement where we go in and check if we actually have some sort of data pulled down from the database because in some cases if I try to write in a username that doesn’t exist inside the database of course we’re not going to have any sort of comment from any sort of users because the user doesn’t exist uh the same thing goes if a user just didn’t make a comment then they’re not going to have any comments so we do need to have something default to show the user if there’s no comments to actually grab from inside the database the way we can do that is just by creating a simple if statement where I go in and say if right now we don’t have anything in inside this array that we just created so variable results so if Mt which is a built-in function inside PHP I can check for if variable results is currently empty so if there’s no data inside this array then we don’t have any sort of data and if that is the case then I just simply want to go in and I want to Echo out a piece of HTML which in this case here I did actually di a little bit inside my style sheet so I’m just going to go and create a div here I’m going to open it up I’m just going to copy this Echo two more times and I’m going to go and close the div down here and then in between here we’re not going to have a div we’re going to have a paragraph and we’re also going to go and close off this paragraph here to make sure that we close it and in between the paragraph I’m just going to say there were no results or something you know you can come up with any sort of message you want here there’s a little bit of freedom in these tutorials so you can just sort of say whatever you think makes sense in this case here so there were no res results um so if we don’t have any sort of results from our database quy then we just Echo this out uh if not and we did actually get a result I’m just going to go and copy this go down below write a else statement then I’m going to actually Echo out uh the user or at least all the comments from the user inside this page here now the way that is going to work is currently we don’t know if we have one comment from this user or we have many comments from the user you know we do have an array here um but we don’t know how many results that might be inside this array either way we do need to make sure that all the comments that we grabbed inside our associative array do get looped out inside the page because we need to Loop out the result so to speak I can actually do something just to show exactly what is going on here so you have a a small idea about what is going on if I were to do a Vore dump and actually go in and refer to my variable results uh before we do that though there is one more thing we need to do inside our little PHP code at the top here cuz I actually forgot about that this D method here we do want to delete otherwise we are basically terminating our entire script and stopping everything from running even if we do grab something from inside the database so do make sure you don’t have that die method up there otherwise it’s not going to work for you uh so we’re going to go back down and then we’re just going to make sure we save everything and going inside our website I can now go in and actually search for a user so let’s go and search for crossing or whatever you might have inside your database there if we were to do that you can see oh okay so we get a search result because we didn’t get our uh small message that we just created we did actually get a v dump here and basically you go in and see that we have a bunch of data we have a lot of data in here and this is actually what we call a multi-dimensional array because we have an array that has a bunch of arrays inside of them so each row is going to be a separate array inside this array that we just created and you can actually see at the very top up here does actually say that currently we have a array that has three other arrays inside of them so that is basically what this says up here uh which means that we have three different rows of data with commments from this particular user again this might confuse you even more than actually help you with anything but let’s go and go back inside our website here and instead of doing a v dump let’s actually go ahead and run a for each Loop because what we can do using a for each Loop which I do think we talked about before is how to Loop out an array so using this can actually Loop out all the multi-dimensional arrays inside this for each loop from inside variable results so what I can do is I can go in and say okay so we do have variable results here so that is the array that I want to grab and I want to have a placeholder that I can refer to inside this Loop here so we’re going to say as variable row so I’m going to grab my variable row and just copy it go inside the loop and then I’m going to say for each Loop when we Loop out one row of data from inside this array with all our data inside of it I want to make sure to Echo out a variable row and then I’m going to refer to a associative name so in this case I’m going to say I want to grab and we can actually go back inside our database here uh I want to grab the username and also the common text and the created ad so I can actually go back inside my code and say I want to grab the username then I’m going to copy this down to the next line and say I want to grab the commentor text and then I want to copy it down and say also want to grab the created uncore at and at this point here we can actually go ahead and test this out inside the browser but we do have two other things that we need to do before we can actually do so the first thing is we need to make sure it actually looks pretty so we need to style it in some sort of way uh the second thing we need to do is to make sure we don’t have any cross-side scripting going on because two episodes ago when we talked about inserting data from your website into the database we talked about the fact that you have to sanitize the data when you actually output data inside the browser to make sure there’s no cross-site scripting happening uh so we do need to make sure that since we’re outputting data right now we’re actually echoing it out inside the browser then we go in here and actually run a HTML special character function to make sure that we don’t have any sort of cross-side scripting happening uh so if a user were to Output some sort of JavaScript inside our database and now we’re echoing it out inside our code here we could actually have a potential issue here we actually allowed the user to Output JavaScript inside our website which is not a good thing uh so we need to make sure we use this HTML special characters whenever you want to Output data from a database inside a website or just any point when you want to have any sort of user data uh spit out inside your website so making sure to wrap everything in HTML special characters is very important or any of the other uh filter uncore input method or something like that that we have inside PHP in order to properly sanitize data now in this case here we’re just outputting string so HDML special characters is the proper one to use here so at this point here I would like to actually test this out inside the browser even though it doesn’t look that pretty yet when we actually output it so if I were to go inside my browser and type in a username that we don’t have inside the database you know because I want to test out the error message then you can see we get there were no results which is good cuz that is what we’re supposed to have but if I were to type in a us that I know we have inside the database for example cing and actually run a search then you can see we get get his entire all the comments that he made inside the database so you can see we get uh one comment up here then we get a second comment over here then we have the third comment over here but as you can see we have three different comments from this particular user here so we do have his comments uh showing inside the website that just not styled yet so that’s the next step we have to do so going back inside the code I will continue styling things here so I do need to make sure because I did actually do that inside my notes here that I wrap everything inside a section tag because my notes say I did that and I did style it you know depending on this so I have to do it otherwise my styling is not going to work so I’m just going to make sure I paste this in and then I’m going to go inside my for each Loop down here and I’m going to Echo out a div because I want to wrap this inside a div just like we did up here I’m also going to close the div right after and then I want to wrap my data inside an 84 and two paragraph tags so we actually have something you know style when it comes to the text as well uh so I’m going to say I want to Echo and I want to Echo out a84 and I’m also going to go ahead and concatenate here to make sure we concatenate everything then I want to go after and say I want to close off my H4 so I’m going to close off the H4 here I’m just going to scroll to the side here so you can see everything and then I’m going to copy paste and do a paragraph for the other two down here so I’m just going to change this to a paragraph paragraph and the same thing when it comes to closing off at the end here but changing it to a paragraph of course like so and I just want to point out here that there is another way to do this when it comes to HTML inside PHP which is not to have it echoed out inside a string uh but actually close off the PHP TX up here for example then you open it up down here if you want to do that and then you can actually just write HTML in between here and then Echo out the data in between the HTML by opening and closing the PHP tags so that is another way to do so but in this cases since we don’t have that much h going on I think it’s just easier to Echo it out so doing this here and going back inside the website just to test how everything looks like I’m going to go back here refresh and again if I were to search for someone that does not exist inside the database for example this random person here then you can see that we have there were no results and it’s been styled because I wrapped everything appropriately and if we were to go back and search for crossing now you can see that we get three different search results and because I styled it and made sure that everything was below each other and wrapped it inside a dip container to have a a white background color everything now looks a lot cleaner and we can also go back and search for another user so if I were to search for besser is cool which is another user that I have inside my database then you can see we get his comments inside his comment table and just like that you created a very basic search system using PHP so that is something you could actually use inside a website if you wanted to um so learning how to do this is a very good and important step to learning how to Output things using PHP when it comes to data uh in the next episode we’re going to talk about something called a session because a session is a way for us to store data inside our uh well inside our session inside our browser uh so we can actually store data from page to page without having to send it using a post or a get method so using sessions is something we use constantly when it comes to PHP in order for the website to remember things as you’re using the web page so this is something that is very cool to learn about so we’re going to talk about that in the next episode but for now this is how you select data and output it so I hope you enjoyed and I’ll see you guys in the next [Music] one today you’re going to learn about something called a session inside your website and a session is very important to know about since it is the way that your website remembers information across all the pages inside your website now we have talked about a post and the get method where we can send data from one page to another page but this is more for when it comes to submitting data from the user or uh submitting a lot of data inside the website that isn’t really permanent but it’s more temporary information that just needs to be sent from one page to another but when it comes to a session this is information that we want to store permanently inside the website or at least for a longer period of time inside the website uh when a user is currently using your website let’s say for example we want to create something like a login system in order to create a login system the website has to remember okay is this person logged in or is he not logged into the website and when you go inside your login form and you type in a username and a password and then you click login then if you typed everything in correctly and you were to log into the website then the website has to remember across all the pages that oh this user is logged in so everything needs to change inside the website so we use sessions in order to store information that has to be remembered per permanently inside our website in order for for example a loin system to work there’s one thing I want to show you before we get started on any sort of code inside our documents here which is to go inside my browser and open up this website that I just created which is completely empty there’s nothing going on in here uh you can actually see that I have nothing in here literally there’s no you know code inside the body tags I do have a second page by the way which is called example. PHP but that one is completely identical there’s nothing inside of it and this is just to kind of demonstrate that it remembers information across pages so I just created a second page called example. PHP uh so you can go and create that one as well if you want to but I do want to show you that if I were to go inside the browser here go inside my developer tool which is F12 on the keyboard or if you were to go inside the website and right click and then click inspect then you also open up that way uh if you were to go in here you can see we have this very typical developer tool that we’ve seen so many times before we have the HTML you have the CSS you can also see the JavaScript uh but if you were to go up here where it says storage and this tab may be in a different place if you’re using a different browser I’m using Firefox in this case here if I were to go in here you can see that we have something called cookies and inside of here you can see we do actually have a cookie that is related to Local Host because I right now have a PHP my admin turned on inside my browser so it it does have a cookie for PHP my admin now a cookie is information that is stored directly inside your browser locally inside your browser not inside the server and whenever you start a session inside a website you actually generate something called a session ID cookie which is going to pop up in here as soon as we start a session because now we’re telling the server that okay so there’s this user here who is trying to remember things about the website and the information is going to be stored inside our server so in order for the server to figure out which person you are CU there might be many different users accessing the same website uh the server has to place a session ID cookie on your browser to figure out which user you are and which session variables you need to have assigned to you so if I were to go back inside my editor I’m going to go to the top of my index page and I’m just going to go and open up and close my PHP tag so we can actually type some PHP in here and I’m just simply going to start a session by typing session underscore start parenthesis and semicolon and with this simple method here we now started up a session inside this page inside our website so right now we don’t have a session going on inside ex example so we could actually go in and copy paste this information paste it over here just to make sure we have a session started on both pages and then with this we’re now going to go back inside the website refresh it and when I refresh it and open up my editor you’ll notice that inside my cookies we now have a second session ID cookie which is called PHP session ID and this is actually the session that we just started inside our web page using the session uncore start so now the server knows okay so there’s a session going on inside this web page which means we need to put a session ID cookie inside the browser so we know which session data belongs to that particular user because like I said many users might be using this particular website here so for the server to pinpoint which user is which we need to have the session ID cookie if I were to go inside and delete this cuz you can actually do that and say delete uh PHP session ID Local Host then the server no longer knows who who you are and all the session data is probably going to get lost so uh this is not something you have to do because the browser actually purchased this so if we to close down the browser it’s going to delete all these session cookies so you don’t need to worry about you know them sticking in here because we do actually have some session security when it comes to sessions as well where people can go inside and hijack your session or something and that is something we need to talk about at some point uh for now we’re just going to talk about the basics of sessions and starting them up and how to delete them again and so on so with this here just know that as soon as you close down the browser this particular session that you have right here or this session ID cookie will get deleted and there is a reason for that that is because the timeout for this particular cookie here is set to a negative so therefore the next time you close down the browser uh you can always you know extend this session ID if you wanted to manually but let’s go back inside our editor and talk a bit about session data or session variables that we can create using the session super Global because we did actually talk about this one many episodes ago but we didn’t really talk about it extensively uh but we do actually have something called a session super Global so if I were to say dollar signore session brackets and then I can go inside the brackets say double quotes and give this some kind of name so I could for example call this one username so if I were to type username and set it equal to something I can for example say Crossing then currently I have a session data or session variable able that is equal to a string called Crossing which means that this information is going to get remembered on your server on any page that has this session uncore start started at the top of the page so what I’m going to do here is I’m going to say that I want to start this session and I want to go down inside my buddy tag and I want to start my PHP tags because I want to demonstrate something for you so we’re going to say we want to start the PHP tags and close it again and I just simply want to Echo out my session variable that I just created up here and pasted in down here so you can actually see that we can actually see this session variable now inside our page but I do also want to take this Echo go inside my example page and show you that we can actually see it inside this page as well so even though inside the second page we didn’t actually set the session variable at the top here we should still remember it so if we were to go inside my browser and say I want to refresh my index page you can see oh we get causing up here if I were to zoom in you can actually see it uh if I were to go to to my other page called example.php then you can see we still get Crossing because we echoed it out on the second page as well without even declaring it at the top because our session remembers oh okay so inside the index page he set this session variable so now we can Echo it out so in this sort of way we can store information inside our session that gets remembered across all the pages inside our website as long as we have this session start declared at the top of the page now I do want to demonstrate something else here here as well which is that if you have a bunch of data inside your session variables how can you unset them and delete the data again because that is also something you need to know about so if I were to type a method called onset and actually take my session variable so if we were to go down here and say we have a session variable called username then I want to paste it in here and unset it so if we were to do this go inside my website you can now see that oh okay so undefined array key username used cuz we don’t know what this variable is because now it’s been onset let’s actually go and go back to the front page because I actually think that makes a little bit more sense since we did set it here uh so if we were to go back inside the front page and set the session variable and then unset it again right afterwards go inside the website go back to the index page you can see it still gives us the same error message because oh okay so we we onset this session variable but let’s go back inside the code and say that what if I have more than just one session variable that I want to delete what if I want to purge all of them uh what I can actually do is I can run a method called session underscore unset parenthesis semicolon and if we were to do this one then it’s going to purge all the session data inside our session so we can’t see it inside our website so would to refresh the browser you can still see that we get this you know we can’t find the session variable so everything has been deleted still so this one here is for deleting all the session data and this one is for for deleting one session data and now we do also need to know how to stop a session from running inside our page so let’s say I have a session started here I can also go down to next line and say I want to run a session underscore destroy and if I were to run this one then we’re actually stopping the session from running again so let’s actually go ahead and go up here and delete this onset so right now we start a session we set a session variable and then I destroy the session inside the same page but now there’s a small thing that I want to show you here because if I were to save this and actually let’s go ahead and go back inside our example page here and just make sure that we only have our session uncore start at the top and also to make sure that we have the echo down here inside the body tag so with that in mind if we were to go inside the browser here this causing variable when I refresh the browser should not be available right CU it just said to purchase all the data so if I were to refresh the page oh okay so cosing is still in here so the reason we can see it in here is because because even though the session destroyed does actually Purge all the session data it doesn’t get purched inside the same page so it doesn’t happen or the the effect is not going to happen until I actually go to another page so if I were to go back inside my uh example page here then you can actually see that when I access this page that oh okay so this username session variable is not available because we did purchase on the previous page using session unor destroy so again if I were to go back inside my my my code editor here session unor destroy is going to purge all the data but you can’t see the effect until you access another page often you’ll also see people use the session uncore unset in combination so whenever you want to completely destroy a session and unset all the session variables this is how you would do it so now that we talked about our session uncore start and how to create a session variable and also how to unset data so I can actually write it in here again there we go so we talked about how to unset data inside our session very Ables and we also talked about how to destroy a session and again just to point it out here sessions are used in order to you know remember information across Pages for example login system or if you have a shopping cart inside your website and the user goes in and puts things in the cart then the website has to remember across all the pages what you put inside the shopping cart so there’s many different things you could use a session for to remember things and of course we do also have some security when it comes to sessions which I think we’ll talk about maybe in the next episode or maybe a little bit further ahead in this course here so having talked about this I hope you enjoyed and I’ll see you guys in the next [Music] video so in the last video we talked a bit about sessions and how we could create a session inside our website and in this video we’re going to talk a bit about session security which is going to be quite a it’s going to be one of the more complex episodes we having discour up until now but I will try to take it and explain it as simple as I can and just show you exactly what we need to do when it comes to basic security using sessions so the first thing we have to talk about is what exactly are we trying to defend ourselves from because we have talked about prepared statements and sanitation you know to defend against SQL injection and cross-site scripting um but what are we defending ourselves against in this video here what exactly I would try to prevent using session security uh something that is very important when it comes to having anything to do with the session is to make sure that other users on other computers are not able to steal our session data so whenever we create a session inside the website like we did in the last episode and we start creating these session variables that are going to store data inside the server then we want to make sure that the ID stored inside the server is only going to point to us who is using our computer so the session ID cookie inside our browser should only match up with the ID inside the server for for us so if another user out there were to hijack our session ID then they can actually go in and steal our session data which is not a good thing so we need to make sure we have some session ID security uh whenever it comes to handling sessions inside the website just to mention a couple of ways that people could potentially hijack your session could for example be using something called session ID sniffing where a user can go in and intercept unsecure trafficking going on inside your website and they basically hijack your session ID and then impersonate you as the user inside their computer and this is why it’s important that whenever you have a session running inside a website that you don’t have a HTTP connection but a https connection another method people use is also something called session ID prediction where basically they try to guess what kind of ID you have inside your computer so if you haven’t generated a strong session ID they can try to predict and guess whatever session ID you might have so it is important that we also go inside our code and generate a much stronger session ID to prevent this sort of thing from happening and then we also have another very popular one which is something called session fixation which is a type of attack where the user basically tries to make you use the cookie that they have on their computer so for example by sending you a malicious link to a website that they actually included the session ID for their computer in so in a situation where you might click on a link that they sent you through for example an email then you can actually go into a website using the session ID that they created so basically you’re impersonating them inside the website but you don’t know it and then of course we do also have cross-site scripting attacks where people try to inject JavaScript into your website to for example steal your cookies so there’s many different ways that people can hijack a session inside a website and we have to make sure we try to prevent as much as possible and just to mention some additional security things that you just kind of need to know whenever you have anything to do with sessions inside the website uh whenever you have anything to do with sessions it’s very important that you always validate and sanitize user data because that is always important to do so whenever the user submit some sort of data make sure you don’t have it be unsecure as you use it inside your website another thing you want to do is also make sure you don’t store any sort of sensitive information inside a session variable for example a user’s address or phone number or email or something like that uh because if a hack were to gain access to all the session data then all of a sudden they have access to very personal information which is not a good thing you do also want to make sure that whenever you have any sort of session data that you don’t need to use anymore that you go inside and you actually delete it because if you have old session data stored in there that isn’t usable anymore then there’s no need for a potential hacker to gain access to information that could have been prevented because you don’t need it anymore inside your website and with that said it is also kind of a thing whenever we have session security going on that the more security you have inside your website the more you’re going to inconvenience the user that is using your website because that kind of goes hand inand and you have to find a balance between how much do you want to inconvenience the user versus how secure should your website be if you want to have the maximum amount of security inside your website you should force the user to log in every single time they use your website but that would also mean that the user has to log in every time they use your website so all of a sudden we have this again security or convenience because you also don’t want to have users being scared away of your website so that’s just a very important thing for me to to just sort of point out there I do also want to mention here that some of the stuff that we’re going to be doing in this video will also be changeable inside the php.ini file inside your server so something that we haven’t talked about yet is that if I were to go inside my exam installation then I do actually have a PHP folder and inside that folder we have something called a php.ini file and this file has a bunch of settings inside of it that you can change in order to do some of the things that we can do in this video here but I do want to do everything using code in this video here just to make sure that everyone who is following can just sort of follow and just write the code down and you know that you don’t get scared cuz oh no we have to go inside a weird file inside our phsp installation how will I do this inside a live server and that kind of thing so I’m just going to go and show you how to do everything using code in this video here now the first thing we’re going to talk about is one of the settings that you do have inside the inii file that you can change using Code which is something called session use only cookies and this is something that goes in and make sure that any session ID can only passed using session cookies and not for example through the URL inside your website because that is one of the ways that people they do session fixation where they go in and try to make you click on a malicious link and they take you to a website and then they might have a session ID stor inside the URL of that link so this is one of the ways we can prevent that from happening so the way we can do that is go inside our website and what I’ll actually do here is I will not start creating code at the top of my index file I’ll actually create a new file inside this this uh root folder here so I’ll create a new file and I’m going to call this one config.php and this is going to be a file that I’m going to link at the top of my index page here so I’m going to say require uncore once and then I want to link to my config do PHP and any other page inside the website where we want to include this code we can just go ahead and require the file just like we did here so what I can do is I can go inside the config file and the first thing I’m going to do is open up my PHP Tags I’m not going to close it again though because this is going to be a pure PHP file and what I’m going to do is I’m going to set something called ini iore set parentheses and inside this one we can set a parameter which is going to be our session uh do use underscore only underscore cookies and if I were to set this one I can also set a value which is going to be one and this is going to mean that we’re setting this one equal to true because one is true and zero is false so in this sorder of way we can go inside that inii file and actually change some of the parameters using Code inside our phsp code uh so what I’m going to do here is I’m going to duplicate this because we do actually have a second one that we also want to make sure we set in here this one is going to be called use strict mode so we’re going to say session. use strict uncore mode and what this setting is going to do is a couple of things or quite a few things actually one of them being that we make sure that the website only uses a session ID that has actually been created by our server inside the website it is also going to go in and make our session ID a little bit more complex when they actually get created uh so in that sort of sense it makes it a little bit more difficult for people to go in and try to guess your session ID inside your cookie so there’s a couple of really good things that this particular one does and this is actually a mandatory thing to have whenever you have anything to do with sessions inside your website so you want to make sure that this line of code is inside your website anytime you have anything to do with sessions the next thing we’re going to do is we’re going to create some cookie parameters inside our code so whenever we start a session inside our website and the cookie is created want to make sure that we do have some parameters set for that particular cookie to make it more secure so what I can do is I can create a function here called session unor setor cookie underscore params and if we want to create this one we can go inside of here we can actually create a bunch of different parameters so I could potentially create a array here and inside this array we can define a bunch of parameters and the first one we’re going to set is something called lifetime now a lifetime is basically going to go inside your cookie and say that okay so after a certain amount of time has passed inside the website we want to make sure this cookie is going to get destroyed and the reason this is important is because we don’t want to have the same cookie running inside the website for too long because if that were to happen it is going to increase the chances of someone catching that cookie and stealing it and if they have that cookie we do also want to make sure that after a certain amount of time they can’t use that cookie anymore so what we want to do in here is we want to set a parameter called Lifetime and I want to point to a new value which is going to be1 1800 which is going to be 30 minutes in seconds so in this sort of way we now created a new lifetime for our cookies so they actually get destroyed after a certain amount of time uh the next thing we’re going to do is we’re going to set something called a domain so whenever we want to go inside and create a cookie it will only work inside a particular domain so in this case here we’re going to point to Local Host because right now we have exam running which is a local host server so this is going to be the domain that we have to point to of course if you had to put this online and you had another website you would call it something like example uh.com for example if it has to work for that particular website like I said in our case here we’re using Local Host the next thing we’re going to create is going to be something called a path and this is going to to point to a sort of path inside your domain here so in our case here we could actually say that it just has to work inside any path inside our website so what I can do is I can say that we want to set it equal to forward slash which is going to be any sort of subdirectory or sub Pages inside our website that is currently running uh inside this particular domain up here our next parameter is going to be something called secure which is going to make sure that we only run this cookie inside acq website so only using a http s connection and not a HTTP connection so I’m going to set this one equal to true then I’m going to say I want to add another one which is going to be H TTP only so we’re going to say h TTP only and we want to set this one equal to true as well and this basically just goes inside our website and restricts any sort of script access from our client which means us inside the browser so now that we have these we can actually go below here and actually start our session so we’re going to say session start just like we learned in the previous episode so all this information up here has to be set before you start the session that is very important to do uh because these has to be set before we actually have a session created but now there’s a couple more things we have to do whenever we create a session and I did actually mentioned one of them which was we need to make sure that the standard session ID created by this particular function here is going to get even better because right now when we create a session ID using session unor start it is going to be a very basic not really a secure session ID so we want to regenerate it into a stronger version which we do actually have a PHP function that can do uh so we have a function which is called session uncore regenerate ID and this particular one if you were to set this one to True is going to just generate a new ID for this particular current session ID that we have so it’s not going to create a new one it is actually going to regenerate the current session ID we have and make make it into a better version however even though we did use this function in here underneath the session story in order to regenerate the ID it is also a very good idea to do this automatically after a certain amount of time has passed inside the website so if a attacker were to gain access to a session ID then after a certain amount of time that session ID no longer works for them so we want to make sure we regenerate this periodically inside our website and I do actually have a blocker code to do that so I’m just going to go and copy paste it in and then I’ll go ah and explain what exactly it does so underneath my session undor start I went ahead and created this blocker code here I’m just going to talk a bit about what exactly it does uh so inside this block of code I have a if and an else statement and inside the if statement I’m basically just checking if we right now have a session variable created using the iset function here inside my session called lastore regeneration if I do not have it created it means that this is the first time I’m running this page inside the website and it’s the first time I’m actually starting up my session and if that’s the case I do actually want to make my session ID Stronger by regenerating it and I do also want to make sure actually create this session variable that we’re checking for up here so the first time we’re running this if statement it will actually go in and create this session variable so anytime other than this in the future it is going to run this El statement instead I did also give this one a value which is going to be equal to the current time that we have inside the server here and this is going to be important for us to actually check if a certain amount of of time has actually passed since we last time actually regenerated our session ID so the current time for us actually regenerating the session ID is going to be equal to our session called last regeneration and inside this L statement I created a variable called interval which is going to be set equal to the time that I want to pass until we have to regenerate our session ID again uh so we want to have this in seconds which means that in 1 minute we have 60 seconds and then I’m basically just multiplying it with the number of minutes that I want to pass until we actually regenerate this session ID so in this case 30 minutes so if you want this to be 10 minutes then you can write 10 if you want this to be 30 then we’re going to write 30 and then afterwards I went ahead and created a if condition which takes the current time and minuses that with the time inside our session variable which is going to give us a number of seconds and then I check those seconds if they’re greater than or equal to our interval and if it’s more than 30 minutes then I’m going to regenerate our session ID and I’m also going to go and reset my last regeneration session variable to be the current time that I now regenerated the session ID inside my website so we’re basically just regenerating the ID inside our session every 30 minutes that is what this code does and with this said you now know some of the basics when it comes to session security inside your website um I do want to point out here that there is more we could talk about when it comes to session security for example creating a new session ID so not generating a new session ID but actually creating one using for example a function called session unor create ID and then you could take this ID and combine it with your user ID from inside the database whenever you create a login system in order to create a unique ID for a login session so um there’s many different things we could talk about for now I think this is good when it comes to beginning and just sort of starting up with session security and just to mention this again for people did miss it at the beginning we can just take this file here that we just created together and just go inside one of the pages inside our website and when we want to create a session inside one of these Pages we just sort of link to this file and then we have all of this you know session security going on inside those pages I do also want to mention at the end here that if you are a channel member then you do of course also have access to all my personal notes here so if you want to have all my personal notes for for example this Paton here with all the comments inside of them then you do have access to these files if you are a channel member and you can find the link for that in the description so with that said I hope you enjoyed this lesson and I’ll see you guys in the next one [Music] so now we reached the point where we have to talk a bit about security when it comes to inserting data into a database because we have talked about creating data inside a database and selecting data and sessions and that kind of thing but we haven’t talked about hashing yet which is something that is very important whenever you want to insert data that is sensitive in inside a database so right now just to demonstrate something if I were to go inside my database that we have created in the past couple of episodes where we created a users table and a comments table if I were to go inside my users table here you’ll see that right now inside our password column we actually don’t have any sort of security going on in terms of Hing uh so right now if I were to you know break into a database like this one right here I could actually see everything in terms of what exactly these passwords are so we can see this user here has a password of 1 2 3 4 this one has a Denny one 2 3 and 1 2 3 1 2 3 4 um but the thing about inserting sensitive data inside a databas is that we’re not supposed to be able to tell what exactly is in there so in order to create another layer of security we do something called hashing which is going to turn our password into a hashed string which is going to be this very long and confusing letters so we can’t really tell what exactly it’s supposed to be it’s just going to be jerish basically so going back inside my text editor here you can see that I have a bunch of files just going and ignore those for now because these are for a example a little bit later on but for now all I have that you need to worry about is a file called hash password. in.php this is a file that I put inside my includes folder inside my root folder just because this is going to be a file that has pure PHP inside of it and we have talked about why I name my files in this sort of way many times in past episode so you’re more than welcome to go back and and just take a look at that if you want to but the important thing about this episode here is just talking a bit about what exactly hashing is so going inside this file here which is completely empty I’m going to open up my phsp tags and I’m not going to close it off again because this is going to be a pure PHP file and we’re going to start by talking a bit about how we can hash and what exactly hashing is now hashing is whenever we perform a oneway hashing algorithm on a plain piece of text for example a password and then we basically convert it into a fixed length string that you can’t really look at and tell what exactly is this supposed to be so it is going to be something that even if a hacker would gain access to your database data then they still can’t see what the data is supposed to be so depending on what kind of data we’re feeding the hashing algorithm it is going to convert it into a different thing so it is something that is going to be different depending on what exactly you’re going to feed to it and it is also important to mention here that a hasing algorithm is designed to be irreversible and expensive to do um so you can go in and make a hassing algorithm more complex if you want to we do also have something called a salt which is a vocabulary word that you may hear from time to time whenever we talk about hashing essentially a salt is a random string of text that is going to be included inside the data that you provide the hashing algorithm so it’s going to mix the salt together with the text that you provided it and then it’s going to Hash it afterward to create a more unique and even harder to crack hashing algorithm to make it even stronger it is important want to point out that we do have many different hashing algorithms out there and some of them are considered to be outdated whenever it comes to for example password hashing uh you wouldn’t use a shot 256 or md5 Hing algorithm in order to do those uh whenever you insert those into a database so there are you know different ways of hashing things depending on what you’re trying to do when it comes to specifically just general purpose hashing when we’re not talking about a password to be inserted inside a database uh we can for example go inside our code like we have have here and let’s say I have a variable which is going to be whatever the user gave us so let’s say there’s a form and the User submitted something to us uh then I could go inside and say I want to create a value called sensitive data just to have something here and I could set that equal to some kind of value so I could say for example Crossing as my username the next thing we would need to do is create something called a salt and a pepper which sounds really funny but just to kind of show what exactly those are um I can actually go and create a variable here I’m going to call this one salt and then I’m going to set it equal to a random string that we’re going to generate based off some functions that we have inside PHP so what I could do here is I could say we have something called bin to hex parenthesis and then inside this one I’m going to generate a random underscore bytes and then we just basically feed this one how many bytes you want to randomly generate inside this function here so we could for example say 16 and by doing this we’re basically just going in and saying want to generate 16 bytes of data data and then we want to convert it to hexad decimals in order to actually have something that we can actually actively use inside our code so heximal representation as it says inside the notes here um so basically just generating a random string so if we were to write some notes Here we could say Generate random salt next we’re going to go and create something called a pepper so I’m going to create a variable and call this one pepper and I’m going to set this one equal to a random string of characters that I decide what is going to be so so this is not going to be random characters this is going to be a keyword that I’m going to use to fuse together with this hash in here uh so I could for example say a secret pepper string so now we have something called a salt and we have something called a pepper and we can fuse these together in order to make our Hash a bit more secure so what I’m then going to do is I’m going to go below here and I’m going to create a variable called Data to hash and essentially we’re just going in and we’ll combine in these three pieces of data up here so we have the sensitive data that the user gave us inside whatever input we have inside the website uh we do also have a piece of data called a salt and then we also do have our pepper and then basically we just go below here and actually run a hashing function that we have inside PHP called hash so what I can do is I can say we have a variable called hash and I’m going to set this one equal to a hash function and inside this one we’re just going to feed it what kind of hashing algorithm we want to use and then the next piece piece of data is going to be what exactly we’re trying to Hash in here so in this case I could say want to use a shout 256 hashing algorithm and then I want to feed it data to hash and if we were to go inside my website here and actually try and Echo this out we can actually Echo this out after each other just to kind of see the effect of what exactly we’re doing here so if I were to Echo out a break just so we have everything on a new line and actually Echo out the hash that we have here then I’m going to copy this line of code and go right behind where we actually combine all this data here and I’m just going to go ahead and Echo out my salt as well so we can see exactly what is going on in here so if I were to do this and go inside my website go inside my includes folder and say I want to access my hash password. in.php file then you can see we get these two here so the first one up here is going to be the random ass salt that we generated at the beginning of the code and the second one down here is going to be the overall hash from combining our salt with the data from the user and our pepper string and as you can see this doesn’t make any sort of sense so even even though everything is combined inside this hash in here even the readable data that we included such as Crossing and a secret pepper string we can’t see anything about what this is supposed to be and at this point he would actually take the salt and the hash and store that inside either a database or inside a file storage system or someplace that is actually secure where people can’t gain access to it and you would actually go and take this salt and has and use that together with new data the user might submit in order to check if it matches up with the old data they subm it up here so let’s say somewhere else inside the website I want to go in and actually first of all grab the data from inside our database so we have the salt and the has stored in this case here we don’t actually have the salt and has stored inside a database and we would technically have to run a query in order to go inside the database and grab those data uh but for now let’s just go ahead and grab the ones inside the code here since this is more or less a example so let’s say I would grab the all up here and the hash that we have down here I would also need to go ahead and recreate the pepper string inside my code here because that is a unique string that I have created inside my code and then we would of course also need to have the data that the User submitted again so let’s say a User submitted this piece of data once more we’re going to go ahead and put it up here so with all this we would now have to combine everything again just like we did before inside our previous script up here so if we were to grab this lineer code where we combined everything and then I would need to take the salt that we just grab from inside our database and replace it with the salt that we have down here and with all these combined we can now run the has algorithm again just to sort of check if it is the same as the previous one so I’m going to copy this line of code up here and I’m going to paste it in we could also go ahead and rename this variable here to something like verification hash just so we know exactly what kind of hash this is so right now we’re trying to verify that the data submitted up here is the same as the previous data so we’re hashing the new data in order to compare it with the old data that we have inside our database so if I go below here and run a if condition in order to compare the these two pieces of hash data that we have uh so the first one is going to be the stored hash from inside the database so I’m going to copy this one put it inside the if condition and check if it is the same as our verification has down here and if it is then we can actually go inside and say we want to Echo something out so we could say something like the data are the same and then I can create a else statement just to Echo something out in case these are not the same so we could say something like the data are not the same so we just Echo that out inside our L statements just to get an idea about what exactly we’re getting in here so with this saved we can actually go back inside the website and refresh it and what we should get is either the data are the same or the data are not the same in this case here since we have cing here and we also have cing up here then the data should be the same as so going inside the website here if I were to refresh it you can see we get the data are the same because they are the same if we want to go back inside my code here and go up and change the value of the new data that was submitted so let’s say a user is typing something in let’s say cosing two and I were to Hash that and you know do everything in terms of the salting and the pepper we hash it and then we want to check if this data is the same as the previous one up here if we were to go inside the website you can see we get the data are not the same because they are not the same because you know the data submitted by the user is something different and it is important to point out here that we’re not deashing anything inside the code we are actually taking two different hashes and checking if these two strings are the same so if we to go inside and change my sensitive data back to Crossing just to give you a example here let’s go ahead and Echo out not the data are the same but let’s actually go ahead and Echo out our actual values here so I’m just going to go and create a break and then Echo out our stored hash and then I’m going to go below here and add another break just so we can see exactly what is going on and then I’m going to Echo out our verification hash then you can see inside our browser if I were to go inside and refresh it that these two strings are the exact same strings because we has the same data in the same way and then we are comparing the two so basically we are getting a true or false statement when we go inside and actually check for these two data being the same data and this is how you can do General hashing inside your website and it is important to point out here that this is for General hashing when it comes to password hasing for a database whenever a user wants to submit a password when they sign up inside a website then we do actually have a different method for doing so and this is something that is much more simple than what we just did here just to point it out this is much more complex but it is important to mention that a hassin method that looks something like this is good for when you for example want to hide certain sensitive data that isn’t specifically password specific this could for example be a name or a email address or you know financial data or something like that this is what you would for example do if you want to just has something that isn’t a password so now let’s go and talk about about how to generate a h password using phsp so if I were to go inside my code here and just delete everything I’ve just created uh for you it might be a good idea to take notes of these and maybe put that in a document on the side of something so you have these saved uh but what we’re going to do here is we’re going to actually do a hash function that is built directly inside PHP in order to Hash a password now when it comes to hashing a password we have a function called password unor has which is going to go in and actually run a in algorithm and a salt that is going to automatically do all of this for us in order to hash everything uh together to a unreadable format so we don’t have to add a salt manually or do a Pepper or something like that this is going to go in and do much of this automatically so in this case here we might say we have a user that submits some sort of password so we have a variable up here called PWD for password which in general is going to be equal to a post method you know from a form where a user submits something to you but now case here since we’re just doing an example I’m just going to go ahe and write cing because I just want to you know just set it equal to some sort of string in here so what I can do is I can take this password and put it inside my password unor has down here and then I can tell it what kind of hashing method do I want to use in order to Hash this password now in general we have two common hashing methods or hashing algorithms in order to Hash our passwords we have something called password uncore default which looks like this so so if we were to say password uncore default which is going to get automatically updated in the future whenever something new comes out inside PHP so this is something that the developers of PHP will actually come out and update as things change in the future so this is not something you have to worry about when it comes to any sort of changes uh but we do also have something called password bcrypt now password uncore bcrypt is right now actually being used inside password default so this is the one that will be used so does really matter if you’re using this one if you’re using password uncore default uh but just know that in the future if something were to change then the password uncore default can go in and change something uh because something has changed but in general right now whenever it comes to password hashing it is recommended to use BCP so this is the one we’re going to be using here and what you can add in here as another parameter is something called a cost factor which is in general how difficult it’s going to be to uh do this hashing uh algorithm here so if you have a hacker that is trying to Brute Force the way inside website by submitting different passwords again and again inside your input field then this is going to slow that process down and make it much more difficult and take much longer in order to actually try to break your hashing algorithm so this is something that is recommended to do and this is also a point where we have to talk a bit about convenience when it comes to users because the higher this number is going to be the more inconvenient it’s going to be for the user because it’s going to take longer whenever they have to log inside the website the general recommendation here is to use a number between 10 and 12 uh but this has to be submitted as an array otherwise we are going to get a error message so if I were to type 12 in here you can see oh it is expecting a array so if we’re to go above here I can go in and create an array I’m going to call this one options and I’m going to set this one equal to an array and inside of here we’re going to add a parameter called cost and I’m going to point to a value so this could be for example 12 or 10 depending on how you want to strengthen the hashing that you have running here uh so I’m going to write 12 and then I’m going to include my options inside as the3d parameter and then you can see we have this uh function here I do also want to take this hash password and set equal to a variable so we can say hashed password and this would actually be the password that we put inside the database when the user is trying to sign up inside the website so let’s actually go and change that up inside the naming up here so we can say this is the password on sign up and we’re going to change that inside our hash down here as well so this would actually be what we have in inside our database so now the next question is what do we do when want to log into a website because now the user is going back inside the website and they type in the username and the password inside the input fields for the login system and we would actually have to take that password and hash it again and compare it together with the password inside our database so what I would have to do down here is create another variable for the new password the user submit in when they try to log into the website and what I’ll do is I’ll go below here and run a password on _ verify which is going to go in and compare the new password that we submitted when we Tred to log into the website together with the old password that is hased inside the database so if were to take my password here put it inside as parameter number one and then take the password up here which is the hash version not the one that the User submitted on sign up because we don’t actually have that stored inside the database we have the hash version inside the database so I’m going to compare with that one and this would actually go ahead and return as a true or false statement and basically just tell are these two the same so if they are the same it’s going to return as true and if it’s not the same then it’s going to return as false so what I can do is I can run a if condition down here where we basically just go in and say we want to check for this particular function here so I’m just going to delete this line of code and move it inside our condition so if right now they are the same I’m going to Echo out they are the same but if they’re not the same then I’m going to Echo out they are not the same so in this sort of way we can very easily just go in and hash a password and then recover it from inside the database and compare that with the new password submitted when the user tries to lock into your website so if I were to go inside my website here and refresh the browser you can see we get they are the same if I were to go back inside my code and change up the password I used when I tried to log into the website so now it’s the wrong password I can go back inside the website here refresh and then you can see we get they are not the same and you can actually see there is a difference when I click refresh there’s actually this short half a second going on before we actually get the update inside the browser if I were to go inside here and change the cost into something like 30 and go back inside my website here and refresh you can see oh still processing still trying you can see it’s it’s moving up here so you can see it’s it’s thinking right now um so as you can see we are strengthening uh the hassing process so imagine you had a hacker that were trying to Brute Force the way in by typing one password after the other inside the website that you have uh then it’s going to take forever whenever they try to do this if you have a cost Factor inside your hessing algorithm now this is taken quite a long time so I’m just going to go and stop it here uh and just change this back to 12 so now your question might be so taking the signup system that we created in that episode number 22 inside this course here how can we take this and implement this inside our signup system because what I have here is a bunch of other files that I did tell you to ignore at the beginning of this l here these are actually the files that I had for my lesson 22 so we have the form Handler that actually takes the data from our index page when the user tries to sign up then we do also have our database Connection in here and then we have our front page that just basically has a sign up form again if you don’t have all of this you can go back to episode number 22 and just recreate all of this with me but what we can basically do is we can actually go inside our form Handler and just include this hashing in between what we’re doing inside this form Handler here so when I go down to where we actually insert some data I want to make sure that I’m not actually inserting the actual password submitted by the user so as you can see we grabb the password after a post method is being used and we’re just taking that password and directly inserting it inside our database which is not a good thing so what I want to make sure I do right down here before we actually bind the data is to make sure we run a hashing algorithm so going back inside my hash password. in.php I can just simply grab my options and my has password copy that go back inside my form Handler and paste that in so now what I’m going to do is I’m going to take the password from up here which was submitted by the user on sign up and replace that with my password sign up down here and then lastly we just take the has password and use that as our binding parameter down here when we actually want to insert the data inside our database so I’m going to replace my password with my H password down here and that is all we have to do so now if I were to go inside my website here and go back inside my index page refresh everything to make sure everything is good go inside my username say I want to sign up as Danny Crossing and I want to create a password in this case I could just say one two three then I’m going to include a email which is just going to be Crossing at gmail.com which is not a real email but just to have something in here if you were to click sign up and go back inside my database you’ll now notice that I have a new user but something is going to be a little bit different instead of having this very obvious password that I can just see with with my eyes inside the database we now have this hash that is going to be in here instead and that is what you want to do whenever you have any sort of sensitive data that you want to store inside for example a database because now if a hacker were to actually gain access to my database data then they don’t know what this data is so with all this here we now talked about just general hashing we talked about how to Hash a password for database and this is basically all I want to talk about in this video here it was quite extensive it is quite complex compared to previous episodes inside this course here but I hope this is something that makes sense to anyone watching this video here so with that said I hope you enjoyed this video and I’ll see you guys next [Music] time today we’re going to do something a little bit different we are going to implement everything we’ve learned up until now in these past many episodes to build a login system so we will actually have a PHP application that can do something inside a website and I do want to point out this is a very long video and I will have chapters in the description so you can actually skip ahead if you want to but just to point out this is going to be a long video but it’s also going to be very worthwhile so if you do actually go through this video you will learn many things so this is going to be a very very good lesson for you to just sort of write notes for and you know just have next to you as kind of like a cheat sheet for something you want to build in the future it is okay to be confused just rewatch certain parts of the video and make sure to write those notes that I was talking about so you will get to a point watching this video where now you know a lot when it comes to PHP I do also want to point out here that there is a tendency for long videos like this for people to get errors as they are following the video and I just want to point out that 99% of the time when people write hey Daniel I copied your code exactly and I’m getting an error message this code is not working 99% of the time it is because of a typo it is because you don’t have the same databases I do so you know some of the columns inside your code has to be changed when it comes to the names and that kind of thing you misplace the parentheses you know any kind of syntax error is 99% of the time going to be the cause even though you copied my code exactly so I just want to point out here look at the error message inside your website and then pinpoint where that is inside your code because it will tell you the line that the error is on and if you’re just completely in doubt and you don’t know anything about where to find that error inside your code you’re more than welcome to share it inside the comments so people can help you out I do also want to point out that we will learn a lot of new things in this episode here so we will for example learn how to combine our user ID from the database with our session ID to make it better for certain things uh we’re also going to talk about error handling which is when you go inside your code and you actually check for errors before you actually run the code because a lot of times you need to run error handlist in order to figure out if something is wrong this could for example be if the user didn’t fill in all the fields inside the form and then they submitted it so you have to check for that and then say hey you forgot something and then you go back to the form to tell them to fill in everything uh speaking about that we’re also going to learn how to write error messages inside our form so if someone doesn’t fill in all the inputs then we’re going to write an error message saying hey you forgot to fill in all the input or if the User submitted a username when they tried to sign up inside the website that already exists inside the database because you can’t have the same usernames then we also want to create an error message so we have to learn how to create error messages and to check for these things using error handlers inside our code to make this into a you know somewhat decent application I do also want to point out that we’re going to do something a little bit weird in this episode here because we’re actually going to organize our code in a MVC pattern which is something that usually don’t talk about until you start talking about object-oriented phsp which is when you start talking about classes and objects and that kind of thing uh but just to kind of get you into the mindset of a MVC pattern early on before we actually get into classes and objects it is a very good idea I think to just kind of touch upon it and teach you the concepts behind it so you’re not completely confused once we actually have to start talking about classes and that kind of thing so learning about the NBC pattern is just a really good idea and I think this is a good opportunity to just sort of get you into the mindset of how to think in an MVC pattern the reason that the MVC pattern is a really good thing for you to learn about is because it is going to allow you to organize your code in a much more scalable way so that once you build much bigger PHP applications then it is something that is going to be much more organized and better to look at because it’s not going to get all you know jumped together and because right now we’re creating what is called procedural PHP which means that when we have someplace inside our website where we need something specific to happen using PHP then we create the PHP code in that place which means that if I have another page inside my website somewhere where I need the same PHP code then I have to duplicate the code put it inside that page too and then you know all of a sudden we have this duplicates of code that is unorganized and you know there’s no need for it to be duplicated so an MVC pattern is a very good thing to learn about okay um so talking about that let’s actually go ahead and get started on this login system we will talk about all the different security things that we have talked about throughout these lessons here of course there is some security that we haven’t talked about but since we haven’t talked about it it’s not going to get implemented in this video here it’s a way for me to say that this is a good start for a login system then you can always work on it later on when you start learning new things so right now inside my website you can see that I have a index page I do have a main.css and a reset. CSS these are just my Styles sheets so if I want my website to look a certain way then I can can Implement a style sheet to do that because this is basic HTML and CSS and this is something you should also know by now uh so we shouldn’t talk about sty teet because it’s not PHP so with that I do want to show how my website actually looks like right now so if we were to go inside my page here you can actually see that we have a login system and we also have a signup system so we do have two forms that basically can go in you can type your username and password to log in or you can type in your username password email to sign up inside the website so just something very basic in here and of course the way I created that is simply by going inside my index page and I went down inside my body tag created our login form and I created a signup form and these are just to point it out very similar to each other so one of them has an input that is called username and it has a placeholder as username so the person knows what to type in here and we do also have a password input and the same thing goes for the the signup form down here we do also have a username we have a password but we also have a email so we actually use an email to sign up inside the website uh for the database I do have a file in here that you don’t need to have called db. SQL this is basically just my SQL code in order to create a table inside my database called users so right now we have an ID username password email and a Tim stamp for when the user signed up inside the website again if you followed my previous lessons you should right now have a database with this exact users table so this is not something you should need need to put inside your database but just to show you if I were to go inside my database here I do actually have my first database in here it is the name of my database and I do have a users table go ahead and ignore the comments table since that has nothing to do with this episode here uh but inside my users table you can see that I have a couple of different users already uh these are from previous lessons inside this course so there’s no need for me to have them in here uh so let’s just go and delete these users we can delete them one at a time by clicking delete or you can take them off here and say delete and now we don’t have any users left inside my users table the first thing we’re going to create inside our login system is going to be a connection to our database since we need to connect to our database in order to actually do something with the database because when it comes to a login system uh we need to be able to store the users information somewhere so the username the password the email that has to be stored inside a database so we need to connect to it so I’m going to go inside my root folder I’m going to create a new folder I’m going to call this one includes and I’m just simply going to put every single pure PHP file inside my includes folder so any sort of PHP that isn’t a direct page inside my website but just has some PHP code that needs to run a script inside of it that is going to go inside this folder here uh so our database file is one of those files so we’re going to go ahead and right click on includes we’re going to go in here and say want to create a new file and I’m going to call this one db. in.php for database Handler inside this file we’re going to open up our PHP code and I want to make sure that we go in and first of all create all our parameters we’re going to create four variables and each of these variables are going to be equal to a string of characters in our case here we’re going to have a host name which is going to be a local host if you’re using XA which I am right now you’re also going to have the database name which is the name of your database which might be different from mine but in my case I call mine my first database and then you need to have a database username and a database password now if you don’t know what your database username password are then you can of course just go inside your database and change these I do have a tutorial on that inside this course here which is a bit further back but we do need to have these parameters here in order to actually quy our database so the next thing we need to have is a TR catch Block in order to actually try to connect to our database so we’re going to run a TR catch block which means that we right now try a bunch of code and if it fails then we want to throw an exception down here now inside the catch I’m going to say that I want to throw a PDO exception so we’re going to say PDO exception you make sure you spell that correctly and then inside of here we’re going to create a placeholder called variable e for exception now we’re just going to go and create the error Miss straight away so we’re going to create a die function which basically just terminates this script if something goes wrong in here and we can actually generate a error message inside this die function so we can for example say connection failed connection space failed colon space and then we can add a message so we can say we want to add our variable e and we want to point to a method called get message and in this simple way here we now have a way for our error message to get displayed if we get some sort of connection error so what I can do now is I can go inside this try block here and I can actually try and connect to my database or at least create a variable that is going to contain a object which is going to be a connection object to our database so what we can do is we can create a variable call this on PDO I’m going to set it equal to a new PDO object and again we haven’t talked about object oriented PHP yet which is okay since that is a little bit further ahead in this course here uh but this is going to be a new PDO object based off a a connection class that is going to allow for us to connect to the database so going inside this PDO object here we can actually give it some parameters so the first one is going to be uh what kind of database are we trying to connect to so in this case it is going to be a MySQL and again just to point it out here some people do keep pointing out even though I keep mentioning it that MySQL databases are not outdated but PHP MySQL functions are outdated okay there’s a big difference between the two uh so MySQL databases are not outdated okay so moving on we’re going to go and tell it what kind of host we’re trying to connect to here and we do actually have our host up here so I can just go and copy my host and paste it down here now something we haven’t talked about that many times in this course here is the fact that when we do this we usually concatenate so you know we do something like this like we just it down here uh but it is actually possible to take a variable and just copy it directly inside the string and because PHP have you know nowadays know how to just look at this and say oh that’s a variable then we can actually do that so afterwards I’m going to add a semicolon and then we’re going to go and add our database name which is also going to be equal to our variable up here which is called database name and then after here we can actually go ahead and say we want to add a comma since now we have to add the last parameter which is going to be the username and also the password so we can say username comma and password so can actually paste that in down here now things are disappearing a bit off screen here so I’m just going to go and wrap everything so you can see everything inside my code I’m going to add one last thing below here which is going to be a couple of attributes for our p connection so we’re going to say we have this PDO connection I’m going to point to a method which is called set attributes and then I’m going to go ahead and add some parameters inside this method here now the first parameters is going to be to set our PDO error mode to exception so it actually works properly inside our catch block down here so inside the parameter I’m going to say PD colon colon a tore e r r m o d e for error mode and then I’m going to set a second parameter which is going to be that we want to set it to an exception so we can say PDO e r r m o d eore score exception and this is basically all we need in order to have a connection going so with this file we can now move on to the next file which is going to be our config session. in the PHP this is a file that is going to allow for us to configure our session so we have different things that can help us make it more secure to run a session inside the website so a hacker for example if they were to gain access to a session ID then we update the ID every 30 minutes to make sure that you know people have less time to do any sort of damage it with our session ID so there’s many different things we can do in here to make our session a little bit more secure uh the first thing I want to do here is of course make a new file in St my includes folder which is going to be a config undor session. in.php so inside this file we’re going to set a couple of things and I’m just going to go and copy paste for my notes here since that is a little bit easier at least for me since I’m teaching this and I have a lot that we need to go through um what I’m going to do is I’m going to open up my PHP Tex first and then I’m going to include two lines of code which is going to go inside our inii file inside our PHP folder inside our server and change a couple of inii settings so right now we are setting our use only cookies and I use strict mode to true in order to make this a lot more secure when it comes to handling sessions this is something that is mandatory so anytime you do anything with sessions make sure that you either change these inside the PHP speed. ini file or you change these using Code like we just did here now the second thing we’re going to do is change our cookie parameters in order to make this even more secure inside our website so I’m just going to paste in a blocker code here essentially this is a function called session unor setor cookie uncore parameters or params which is going to accept an array inside of the parameters that allow for me to change things like the lifetime of the cookie we can change the domain this cookie should work on any sort of subpaths inside this domain here that it should work on in this case I’m saying it should work on any sort of path inside this domain here uh we can also set secure to True which is going to allow for us to only use this cookie inside a secure connection so an https connection and we’re also setting HTTP only to true to avoid the user being able to change anything about this cookie using a script for example JavaScript inside our website so there’s a couple of things we need to set using this function here in order to make things a lot more secure when it comes to a session again we did talk about this a few episodes ago so if you missed that one you’re wouldn’t welcome to go back a couple of episodes to our session security video um we do need to add in one more thing which is going to be a uh if condition that is going to run a update every 30 minutes which will go in and take our cookie and regenerate the ID for that cookie and this is something that helps us prevent attackers from Gaining access to the cookie and then using that cookie for more than at least you know maximum 30 minutes so what I’m going to do here is I’m going to create a if condition which is going to go in and check if a certain session variable exists inside the website because if it does not exist then we need to create it in order to check when we last updated our session cookie so what we can do here is I can go inside this if condition and let’s go and wait with the um condition parameter for now because that just confuses people uh so inside the actual brackets here what I’ll do is I’ll add in a couple lines of code the first one is going to be where we actually go go in and grabb our session and run a function called session uncore regenerate ID which goes in and regenerates our session ID to make it even better and more secure because the default one that you get from doing the session _ start is not very good uh so doing this here is going to allow for us to regenerate it and right now it may be asking what session because we didn’t actually start any session yet uh so let’s go and do that right now let’s go and put in a session unor start right Above This condition here so right now we started a session after we set all these parameters of course and then we went in and said we wanted to regenerate our session ID to make it better underneath here I’m going to say I want to create a session variable so I’m going to say we have a dollar signore session going to call the session variable something like last regeneration so lastore regeneration and I’m going to set this one equal to something called time now time is a function we have inside PHP that just simply gets the current time inside the server uh so by setting last regeneration equal to the current time I can now check when is the last time we actually uh went in and updated our session ID because that is going to be the time stamp inside this last regeneration session so what I can do now is I can go inside my if condition and say I want to run a is set function that goes in and checks if this particular session exists inside my website because if it does not exist it means that we have not yet went in and improved our session ID so that is something we have to do uh but right now we’re checking if it does exist and we have to check for the opposite so in order to do that we write a exclamation mark in front of the is set function in order to do the opposite so basically we’re checking if it does not exist inside the website so now what we’re going to do is we’re going to go and create a else condition which is going to go in and update our session ID after 30 minutes so what we need to do here here is we need to go in and say we have a variable called interval and this interval is going to be equal to the time in seconds that I want to pass until we have to update our session ID again so this case here there is 30 seconds to a minute so we need to say 30 times the number of minutes that we want to actually pass so in this case it’s going to be 30 minutes uh so what I’m going to do below is I’m going to run another if condition because now we want to actually check if the current time minus the time inside our last regeneration is greater than or equal to 30 minutes because if it is then we need to regenerate the ID again as so go inside this condition here we can say if our time which is that function we did before as well minus our session called last regeneration is greater than or equal to our interval that we set up here and if it is then we want to regenerate the ID and we also want to reset our L generation equal to the current time that we now again updated our session ID so I’m just going to copy these two lines of code here and paste them below but now there’s an easier way to do things cuz now we start getting into functions because we are duplicating code so these two lines to code here are also down here so you know instead of having to duplicate the code every single time uh we could go below here and create a function and I’m going to call this function something like regenerate session ID so now we have a regenerate session ID function and I can take these two lines of code paste them inside the function and simply run the function inside where we need to have these two lines of code so we can just paste that in there semicolon copy this line of code and paste it inside up here instead as well so now we are running the same code but we’re not duplicating code in multiple places so going back inside my index page we’re now going to talk about these forms down here because now we have to start creating the signup form and the login form and you will notice that inside my sign up form and inside my login form I do have a action that points to a file that we do not have created yet so we need to create these two files inside our includes folder so what I’m going to do is I’m going to right click on my includes folder create a new file and I’m going to call it login. in.php and I’m also going to create a file called sign up. in.php so now we have the file that is going to actually run the code for signing us up and logging us in once we actually do get to those parts inside the website here for now let’s go and close down to login. in.php file since we don’t want to talk about the login system until we actually created the signup system because the signup system comes first right we need to sign up first and then log in the user uh so let’s go and start with that inside this page the first thing we’re going to do is we’re going to open up our PHP tag so we can actually run some PHP code in here and then we’re going to run a if condition to check if the user actually accessed this page legitimately so did they actually submit the form in order to get to this page or did they go inside the URL and try to access this page in a weird way by you know trying to access it directly inside the URL uh because that is something we can do so what I want to do is I want to go inside this condition here and I want to say I want to run a dollar signore server super Global and I want to check for a request method which is going to be a post method so we’re going to say requestor method and I want to check if it’s equal to a post method if the request method is equal to a post method it means that the user did get here correctly if not then they did not get here correctly and we have to send them back to the front page so that is something we have to do now so if they did not get in here correctly then we run a else condition and then we just basically run a header function in order to send them back to the front page so a header function looks something like this uh we just basically go in and tell it where to send a user so that would be a location which is going to be back One Directory because right now we’re inside a includes folder with this file here so we go back one directory and then access the index.php file in order to send them back to the front page I’m also going to go and add a die function underneath here to make sure that this script stops from running so if there’s any other code in here that might accidentally get run uh then we do actually stop the script from running so that is a good thing to include in here so the next thing we have to do inside our if condition here is to actually grab the data from the users so inside my uh sign up form I did actually have a post method that is called username because that is one of the inputs that was submitted and I’m going to set this one equal to a variable which is called username so again just in case people are confused if I were to go back inside my signup form you can see that down here I do have an input and this one is called username so that is why I’m referencing to a username post method because that is the method we chose for this form here so going back inside the sign up page I’m going to duplicate this down two more times since I have two more inputs one is called PWD for password and the other one is going to be called email we’re not going to do any sort of Sanitation here by the way because I do know that some people may be pointing out that we have HTML special characters or you know some of the other filter underscore inputs that we can use in order to check for certain things but it is best practices to not do this until you actually output something inside the website or you try to store information inside your website uh so we’re not going to do HTML special characters until actually output something underneath here I’m going to go and run a TR catch block just like we did inside our connection file and we’re basically going to do the same thing here so we can actually go inside our database connection and say we want to you know actually run a exception and a error message of something would have failed so we can go back in here and say we want to replace that uh so basically we want to run a PDO exception and we want to have a variable e which is the exception and then we can run a error message to say something like query fail instead of saying connection failed so the first thing we’re going to do in here is I want to require underscore once my database file because we do actually need to have the database Connection in order to actually connect to the database I’m going to link to my db. in.php file we do not need to say it’s inside a includes folder because we’re already inside the includes folder so there’s no need for that um and one thing I want to point out here is we will need to require some more files because I did talk about something called the NBC model which is something that we should talk about just a little bit this early on because I want to get you into the habit of thinking in a MVC pattern which is going to be very beneficial for you a little bit later on in this course here and in any other courses you might be taking inside YouTube uh because the MC pattern is something you just need to know about so before we continue here let’s actually go and create our files for the MBC pattern so I talked about the Mec model being a way for us to structure our codes which is going to allow for us to make is a lot more scalable uh a lot more organized inside our code and in order to do that we’re going to create a lot of functions that are going to have different purposes so for example we’re going to have one file that is going to have functions inside of them that are used to connect to a database and actually cor the database then we’re going to create another file which is going to be used in order to actually show data inside our website so whenever we have any sort of PHP code that is going to show something inside the website that is going to go inside the second file and the third file is going to be for actually handling any sort of input or information that needs to get run inside the website and this is what we call a MVC model we have model U controller and whenever it comes to any sort of application like we’re doing right now so right now we have a signup system and we have a login system so we’re going to go in and create a MVC file for our signup system and for our login system so going inside our directory here I’m going to go inside my includes folder I’m going to create a new file I’m going to call call this one sign upore model. in.php I’m going to create a second file which is going to be called sign upore view. in the PHP and I’m also going to create a third file which is going to be called sign upore controller. in.php controller is spelled c n t by the way so now with these let’s go and wait with the login system because like I said that it’s going to wait until a bit later uh so for now what we can do is we you can actually take a look at these different files so right now if I want to go inside my model here you can see there’s nothing inside this file uh basically these three files are going to have a bunch of functions inside of them and depending on the file we’re going to have these functions do a specific thing so they have a certain task they need to do uh so inside the model here that is going to take care of only cing the database and getting data or submitting data or updating data or deleting data it’s only going to interact with our database and it’s very important to point out here that these are very sensitive functions because these are interacting with our database and that is a sensitive thing uh so the only thing that is allowed to interact with these functions in here are going to be our controller file because our controller takes care of handling input from the user and then uses these functions in here to actually send that information to these functions so they can actually do something with the database so we separating different tasks here and I know that may sound confusing but that is something that is going to be something you have to learn to do so what I’ll do to start with inside this model here so I’m going to open up my PHP tags and I want to say I want to activate something called strict types and this is something you don’t have to do this is something that I like to do because it just sort of prevents more errors from happening inside our code if it were to write a typo or something or submit the wrong type of data so by declaring that we want to set our strict types equal to true it means that we are allowing our code to have something called type declarations I will talk more about type declarations once we actually get to it for now just know that this is something we’re going to have inside our code here and what I’m going to do is I’m just going to go and copy this code make sure I save it and paste it inside my controller save it and paste it inside my view because we’re going to have this inside every single file so going back inside our sign up that ink the PHP file we’re going to include two more files underneath our database connection the first file is going to be the model that we just created so we’re going to say we have a sign upore model that ink the PHP and then we do also have a signup uncore controller that ink the PHP the order here is very important because that is going to be relevant for later when we do talk about object or PSP and then instantiating different objects based off classes uh and that has to be done in this order here so this order here is just a good habit for you to get into it uh so the model is always going to come first after the connection and then we’re going to have the controller after the model here in a situation where you did also need to connect to the view that would actually go in between these two so you would have these sign of view in between the model and the controller just to point it out but for now we don’t need to have the view we’re just going to go and delete it here and again if this is confusing to you about model view controls just go and wait because it will make sense in a second I promise okay I know it doesn’t make sense when you get started on it and I’m sitting here talking about it uh but since we haven’t actually done any sort of code based on it yet it is going to sound confusing so you’re not alone if this is confusing you it will make sense in a second okay so going below here the first thing we’re going to do inside our Tri block here after actually grabing these files is going to be running error handlers now error handlers is a way for us to go in and actually do any sort of prevention to make sure that things are running appropriately so to speak so things are running correctly inside our code uh so if the user were to go inside your website and not type something inside one of the inputs when they’re trying to sign up then we want to take them back to the front page and say hey you forgot to sign something up in there so that’s the thing we have to to do in order to make sure that we have some sort of error prevention happening inside our code uh and some of you may say well Daniel can’t you just go inside your index. PHP and go inside one of these inputs here and include a required attribute and with this attribute we can no longer submit this form right uh wrong because this is front-end development this is client side languages so HTML CSS JavaScript cannot be used when it comes to security at least we cannot rely on them when it comes to to security that should always be done using a server side language because if I were to put this inside my website I can just go inside my website here I can click F12 to go inside my developer tool go inside my form down here and I can just go ahead and remove my required attribute and there we go I can now hack this website and bypass it so it’s very easy to do so do not rely on HTML or css or JavaScript for any sort of security only for extra things so to speak so having said that let’s go and go back inside our sign up and actually create a error Handler for checking if every input are actually filled out before the user actually submits the form so what I’m going to do is I’m not just going to write the code directly in here because that is not organized that is not the appropriate way to do things because all of a sudden we have a lot of code inside this file here uh so let’s go ahead and split up the task to our different files so now you have to ask yourself okay so what are we trying to do here are we trying to quy the database are we trying to show something inside the website are you trying to take some user data and do something with it yes we are so we’re going to go inside our controller file which is up here so our sign upor controller. in.php is what we have to use so inside my controller I’m going to go and create a function which is going to have a name that name is going to be is input empty so isore input put underscore mty and this is just you know one of many ways you could name functions just to point it out some people prefer to use camel case other people like to use underscores so this is just a little bit easier for people to tell what this function is at least from my eyes when it comes to naming these functions here uh but inside this function what I can do is I can pass in some parameters so inside my parameters here I want to pass in a username so going to say we have a variable called username I do also want to pass in a password and a email because I have to check if any of these were not submitted when they us to submitted this signup form and remember we use these parameters here to pass in data from outside the function so this is important to have uh so inside the function itself I’m going to run a if condition and inside this if condition we have a built-in function called empty which is basically checking if one of these variables are empty or if they have data inside of them so I’m going to grab my username put it in here and check is this one empty or is another one of these empty so I can actually copy paste here and just simply check for the password I can also copy paste again and then we can check for the email and these two pipe symbols here means or inside our condition so if this one is empty or this one is empty or this one is empty then we want to run a certain blocker code so in this case here this would actually mean there is a error inside our form because they did not fill in one of the inputs if this one is actually true uh so what I’m going to do is I’m going to go in here and say I want to return a true Boolean and then I’m going to run a else condition and say I want to return a false Boolean and in this sort of way we can now go in and actually check is there a error when the person tried to submit their username password and email because they did not fill in the inputs so now saving this I can actually go and copy the name I can go back inside my sign up. the PHP file and inside my error handlers here I’m going to create a if condition that is simply going to check if this one returns as true or if it returns as false because if it returns as true then we do actually have a error inside the website so in this case here if this returns as true by just pasting in the function and at this point here I’m actually going to wait with putting in the code for the actual error message inside this condition here because we do have a couple of different ways we could do it and I do have a way that we are going to do it in this video here uh but it’s not going to make sense until a little bit later on so let’s go and wait with this uh what I’m going to do is I’m going to create a second function that is going to check for something else in this case we can actually check if it was a valid email that the user actually submitted so what I can do is I can go back inside my controller I can copy this function here just copy paste it below and I can call it something different so in this case if you could say something like is email invalid isore _ inval valids and now at this point here we’re only checking for a email so we don’t actually need to have the password or the username so we just go and delete those two from the parameters here and inside the condition here I’m just going to go and delete these empty functions because we’re not going to check if they’re empty we’re actually going to run a filter so we’re going to say filter uncore V which is a build-in function inside phsp and inside this one we can tell it what do we want to filter for so in this case here I want to filter my email so I’m going to paste in my email as the first parameter and then I want to tell it what exactly do I want to filter here and in this case I want to filter to validate if this is a proper email so filter uncore validate uncore email and this is actually a built-in uh validation inside PHP so you can actually check if this is a valid email uh if that is the case then right now we’re returning this as true and if that is not the case then we return it as false but this is the opposite of what we want to do we do actually want to say if this is a invalid email then return as true so we’re going to go in here and write exclamation mark which means the opposite so in this sort of way we now have a function that can check if the email is actually valid and if it is not a valid email then we return this as true so again we can go and copy this go back inside our sign up the dink to PHP so I’m just going to go and paste in my function below here and copy the if condition paste it down below and simply take this email invalid function and put that inside instead inside this condition here here uh so the next thing we can do here is we can actually go ahead and check if this uh username that the user tried to submit has been already taken because inside the database you may have many different usernames and you don’t want to have two users with the same username uh so what we can do is we can run another function inside our controller in order to check for a certain username and if it exists inside the database then the users should not be allowed to use that username so again we’re going to go inside the controller and copy this function and just paste it down and we’re going to rename it into is username taken and we’re just going to go and delete the parameter inside our if condition here and at this point I do actually want to point something out which is that right now we want to run a function that has to go inside our database and check for a username which means that we have to query our database but now I did talk about this earlier I said that we have three different files we have one called a model that takes care of cing the database then we have the view that shows information inside the website and our controller takes care of any sort of information so in this case here what do we do do we go inside this controller here and then we quy the database because that’s a no no only one file is allowed to actually quy the database and interact with the database inside our code which is our model file uh but before we do that we do actually need to do some type declaration here because we didn’t actually do that uh so right now our username up here is actually supposed to be pass in as a string data type or at least that’s what I decide this is going to be because the username is a string of characters so that is going to be a string data type I’m going to do the same thing for our password and our email since that is also going to be a string then I’m going to go below here inside my email invalid function and do the same thing because this is also going to be a string going to go below here and say I want to pass in a username that is also going to be a string so this sort of type decoration here is not something you have to do but it just adds another layer of error prevention inside your code because if I were to pass in a Boolean inside my uh function here then it would tell me inside the website hey that is a incorrect data type so it’s just an extra layer of error protection you could also go after here and say what the return type should be so if this has to return as a Boolean meaning a true or false statement then I can say bull right after here and say that is going to be the return type uh but let’s not go into that right now let’s just go and talk about telling it what kind of data we expect inside our parameters here so having talked about that let’s actually go back inside our model page and continue what we’re doing right here so right now we’re trying to query the database in order to check if a username is already taken uh so going inside my model page we’re going to go below here create a function give it some kind of name in this case here I could call it getor username and I can go ahead and say I want to pass in a username because we need to use the username in order to actually C the database to tell if the username exist in there uh so we have to pass in a username again remember we’re using strict types here so we can actually say we want to declare this as a string called username so now at this point here we do actually need to run a PDO Connection in order to do this and right now our PDO connection is inside our db. in.php file so there’s a couple of ways we could do this we could go in here and actually go ahead and require our file here so require uncore one and link to our db. into phsp but that is not actually necessary because we do actually have that linked inside our signup page so at the top here we link to these files up here we do have the database connection linked up here first before we actually access these files down here uh so going inside the model here I can actually go and pass that in inside as a parameter here so this is going to be a object data type so not a string or bully and this is actually a object data type uh because this is a PDO object that is going to to connect to our database again we haven’t talked about object or ENT PHP so just for now just trust me this is a object data type uh so this is going to be our variable called PDO which is of course the same variable we have inside our dph that in the PHP which is right here so now all we need to do is actually query the database because now we do have our connection passed into this function here so going down I can say that we have a variable I’m going to call this one query I’m going to set it equal to a string which is going to be the query for our database so in this this case it’s going to be a select statement I’m going to select my username from inside this table here because that’s the only thing we need to check for so there’s no need to grab all the data in this case we can just say username from our users table and then I want to say where a username is equal to a placeholder that we’re going to call username and in this sort of sense here remember the semicolon at the End by the way uh we now have a query statement that we can actually quy into the database using our connection but we do need to do this in a secure way using prepared statements so below here I’m going to create a variable and call this one stmt for statement and we’re going to set this one equal to our PDO connection and then we want to point to a method called prepare which means that now we’re creating a prepared statement and I’m going to pass in my query and send this one in separately because by sending in our query separately from the actual data from the user we now separate the data from the actual query which makes this something that is going to prevent SQL injection which is a good thing so having done this what I can now do is I can actually bind the data to this quy and send that separately so first of all we have to bind the data so we’re going to say we have our statement point to a method called bind param parenthesis and inside of here we have to tell it what is the name of the placeholder in our case it is going to be username so I’m going to paste that in as a string and then I want to to tell it what is the data I want to put in where this placeholder is in this case it is going to be our username and then we simply run a execute statement so we want to take our statement here point to a execute method and this will actually go in and actually Cory our database using this SQL statement up here uh so now that we did that we need to actually check if we did actually grab a row data when we search for a username called whatever the user typed inside the input uh so in order to do do that we are going to write a variable called result and we’re going to set it equal to our statement and refer to a method called Fetch now fetch is only going to grab one piece of data from inside the database so it’s not called Fetch all it’s just called Fetch so in this case we’re just grabbing the first result now the fetch type is going to be a PDO type and I want to refer to fetch uncore asak which means that I want to fetch this as a associative array this basically means that we can refer to the data inside the database using the name of the column inside the database which is a much better thing than using index arrays uh so in this sort of way here we can now take this data and return it so we’re just going to go and return our variable result which means that when we run this function here we grab the data or if the username does not exist inside the database then we get a false statement so now we copy the name of this function we go back inside our controller and we paste that function inside our if condition condition now we do need to remove these type declarations in here so we’re just going to say we don’t want to have those because we don’t need them when we actually call upon the function we do also need to make sure we pass in our connection inside this function here otherwise this is not going to work so we do need to say we want to pass it in and we want to make sure this is a object type so doing this here is basically going to go inside the if condition and then check is the username inside the database if it is then we return this as true if it is not then we return this as false meaning that this is going to be a error if the username is already taken or not an error if the username is not taken so again we can go back inside our sign up the D the phsp go down and copy one of these if conditions paste it below and change the name to what we called it inside our controller so we just copy the name of this uh function here go back inside and paste that inside our if condition of course again remove the type declarations because we don’t actually need those from be used to functions and in this sort of way here we now have something that goes in and actually checks for a username so now I’m going to do one last thing inside my error handlers here and it’s very important to point out that you can do many different types of error handlers you can also check for the length of the username or the password or the email or whatever um I’m just going to go and do a couple of different ones here and then you kind of get the idea that we can just do this and you can come up with any sort of error Handler WR a function for it and then refer to it inside a if condition inside our sign up that in the PHP file uh so it’s the the same process every single time basically uh so what I want to do here is I want to copy paste my if statement because I do want to include one last eror Handler I’m just going to go and delete the function from in here because we don’t have it yet and I’m going to go inside my sign upore controller and I’m going to create the last function so in this case I’m just going to copy paste one of these function here I’m going to change the name so it’s not is username taken but is email registered in this case here we do also need to Cory the database because we have to check for a email so I do also want to make sure I include my database connection inside this function here I do also want to make sure I pass in my email so not the username in this case so the email and inside the if condition here we’re just going to go and delete our function because we want to run a different function now so again because we have to quy the database to see if there is an email inside the database we have to do the exact same thing as we did with the username just using a email instead uh so we do actually need to go back inside our model and create a SE function that is only going to check for a email so in this case if we can say get email and all we have to do is change a couple of different variables in here so right now we’re passing in a email I want to go inside and take my email column I do also want to change the parameter here or the placeholder to email I do also want to change the name that I’m binding inside my bind parameter method down here so it’s going to be the email variable and also the email placeholder and with this this is all we needed to do in here here so we can go and copy this go back inside our controller and paste that inside our if condition and of course do also make sure you remove these different type decorations because we don’t need those inside the function when we call upon the function and with all this we basically now just check for an email being inside the database because it was pretty much the same thing as before uh so it’s very easy to do so going inside our signup we can go ahead and copy the name of this function here and paste that inside our condition and of course remove the type Creations like so and at this point we need to talk about how we can actually create these error messages because now we have a bunch of functions that check for different things and if one of them return as true it means that there is a error so what I can do is I go above my different conditions here and I can create an array which is going to be empty I’m going to call this one errors so we’re going to say we create a errors array which is going to be equal to no data and what we basically do is if there is a error inside one of these conditions down here then we assign that error inside this array up here so we’re going to stack a bunch of data inside this array depending on how many errors we get inside our code and then at the end if we have any sort of Errors inside the array then it means we get a error because there is an error in there and then we’re going to prevent actually signing off the user because if there’s an error then we shouldn’t sign up the user uh so what we can do here is I can actually go ahead and go inside the first condition and say I want to actually assign a piece of data to my array so I can say we have a variable which is called errors and I want to go ah and say brackets is equal to some piece of data which in this case is just going to be a string so right now I have to tell it what is going to be the associative name for this particular piece of data so this is the key this is the value so if I want to grab this particular error message here then I need to refer to this particular key in here that is basically what is happening again if you’re a little bit confused about when it comes to arrays I do have a array episode where I talk about arrays extensively um so this should be something you know by now for now let’s just go and go inside our array and create a key for this particular piece of data I’m going to call this one empty input so _ input and I’m going to create a error message so in this case we can say something like fill in all Fields exclamation mark then I’m going to do the same thing for all these other ones down here so I can go down and paste in my code and I can change this one not to empty input but instead we can say invalid email invalid unor email make sure we actually spell that correctly invalid change the error message to invalid email used then we’re going to go inside the next arrow down here which is if the username is taken and I can go ahead and save we can call this key something else so this is going to be username uncore taken and I’m I’m going to call the error message something else username already taken exclamation mark then we can go inside the last one down here and say this is going to be email uncore used and then we can change the error message to email already registered and at this point here if there was no errors meaning that none of these actually returned as true and insert this data inside our array it means that our array is going to be empty because if there’s no error then we didn’t put any data inside our array so now all we have to do is run a condition and check if this array is empty if it is then it means we had no error messages but if it is not then we need to send the user back to our index page you know what the sign up form is with error messages so I’m going to check if we did have errors this is going to return as true by the way if there is actually data inside this array if there’s no data then it returns as false and this is going to return as true if there is data inside this array and it’s going to return as false if there’s no data inside this array so what I can do is I can go inside this condition here and I’m actually going to go and create a session variable so I’m going to say we have a dollar signore session and I’m going to set this one equal to error signup or at least call this one error signup and I’m just going to go and assign some data to it which is going to be all the error messages that we have stored inside our errors array so I’m going to assign that to it but now at this point if we don’t don’t actually have a session started so we can actually run these error messages because we need to actually have a session started before we can actually store session data inside a session so what I can do right above where we actually assign these errors to our session variable is I can actually link to our config file because that has a session started uh inside that file so I’m going to go up here and just copy paste my require go down below paste it in then I’m going to change this to config uncore session in PHP because like I said inside this file here we do actually have a connection started so we could just run this liner code so I could just take this go back inside my sign up and just replace that with this line of code instead but because we do have a much safer way of doing things when it comes to sessions inside that file so instead we can just link to that file in order to start a much safer session so with this in here we can now actually assign this session variable to a value and have that stored inside our session so now what I can do is I can actually go below here and I can link back to our index page where we have our sign up form because we need to go back to that page and run some error messages so I’m going to run a header function and I’m just going to go ahead and copy paste what we have down here since that is going to be the same thing so we’re going to go back to the index page and because we have all the errors stored inside our session we can actually go ahead and print those out inside our page so let’s actually go and go back inside our index page and do that right now because why not we are at the error messages so why not just show them already so right underneath my sign up form I’m going to run a PHP function that we haven’t created yet so I’m going to say we have our PHP Tags I’m going to close it again and I’m going to refer to a function called check uncore sign upore errors parentheses semicolon and now because we’re actually outputting something now using PHP from the signup system we’re now going to jump inside our view. in the phsp file so our sign upore View uh is going to have a function inside of it which is going to be what we just created inside our index page so we’re going to say we have a function we’re going to call this one check uncore sign upore errors then we want to open up the actual function here and inside this function I first of all want to run a if condition because I want to actually check if we have these errors stored inside the session because if there’s no error stored inside the session it means there’s no error messages so inside the parameters we can run a is set function and actually check one of these session variables so in this case here I’m going to check if we have our erasor signup which is going to be the session variable we have inside our sign of the in the PHP that we did actually create down here and if we have that I want to go inside this condition and I want to create a variable called errors and I want to set it equal to our dollar signore session actually we just go and copy paste from up here which means that now we have a variable inside our PHP code which is equal to this array that has all our errors inside of it uh so this is actually a array now now the reason we’re doing this is because we do actually want to unset our session variables because like we talked about in previous session security episodes as soon as you have data inside your session variables that you don’t need to have anymore you need to make sure you actually delete them uh so at the bottom here I’m going to run a unset function that is going to unset my session variable because once I’m done running this script here I don’t actually need this data inside our session anymore so we’re just going to go ahead and remove it again right afterwards uh so in between here we’re going to go ahead and actually run the error code so first of all I’m going to run a small break just because I want to get some distance from the actual form uh this could also be done using HTML if you wanted to but inside my code here I’m just going to go and create a break and then below here I’m going to run a for each Loop because that is a loop type that is used in order to Loop out a array so I’m going to grab my arrow array and say I want to refer to that one and then I want to say as variable error which means that this is the placeholder for one of these data inside uh this array here and inside for each Loop I’m just going to go and Echo out a paragraph again I’m just going to copy paste from my notes here so I have a paragraph that has a class to it because I have some CSS inside my my website here and in between these paragraphs here I’m just going to go and concatenate my code so I’m going to say I want to concatenate and I want to go ahead and refer to my error and in this order way we now Loop out every error message below each other inside this page here so at this point here we do actually have a error system working now we could also say sign up success once we’re actually done uh but let’s go and wait with that until the end because we do actually have a little bit more code inside our signup in.php file and once we’ve done that then we can return to this in order to actually run our successful message so now in order to test this out we’re going to go back inside our index page and actually link to our file because right now we don’t actually have our sign upore view. interface P linked inside our index page uh so in order to actually access the functions we need to go at the top here create a pair of PHP opening and closing tags and then we’re going to say we want to require underscore once and I want to link to my includes folder and inside the includes folder I have a sign upore view. in.php we do also need to make sure we have a session started otherwise this is not actually going to run this code because we need to have a session available to us to grab the error messages so I’m also going to link to my config file so I’m going to go inside my includes folder link to my config session. in the PHP and doing this we can now go back inside our website and actually test this out we’re going to go and refresh the browser just to reset everything and now if we were to go inside my sign up form and try to sign up without typing something inside my inputs you can see we get fill in all fields and invalid email use because right now uh we didn’t type in a proper email and we did not fill in everything inside the form uh so if we were to type something in so for a username a password a invalid email you can see that oh we get a invalid email use because that was actually wrong if I were to type in a non-existing username from inside my database a password and a correct email so I could just say example at gmail.com and then click sign up then you can see we get something you know it actually puts us to the next page because now we type something valid into our signup form but of course we haven’t yet created something to actually sign us up inside the website so right now it’s not going to do anything now we’re just stuck inside our sign up the thing the phsp page um but just know that now we have something that actually writes out error messages once we do actually create some sort of error inside our code so back inside our sign up the link the PHP file we can actually continue in order to actually sign up the user inside our website uh so what I’ll do is I’ll go below here and I’m going to run a function called create uncore user parenthesis semicolon because at this point here we don’t actually have any errors CAU by the error handlist that we created inside our code uh so we can actually go ahead and run this function here so in order to do that we need to go inside our controller I’m going to copy paste one of the functions and I’m going to call this one create uncore user which is the one that we just created inside the signup page and I’m going to paste in our connection because that is important and then we do also need to paste in all the different data that the user actually submitted so I’m just going to go and copy pasted here so we need to paste in the password the username and the email and then going inside our actual function we can go and delete what we have here and we’re just basically going to run a function from inside our model page so we’re going to say we want to run a function called setor user parenthesis and then we’re just going to go and paste all the same data as we pasted inside this function here so we’re just going to go and paste that in make sure we remove the type declarations just like before we’re going to save this and then go inside our model page and then we’re actually going to to create a new function and we’re going to call this one the same thing as we did inside the last function so setor user and of course paste in all the data including our type declarations uh inside the parameters here curly brackets and then we can actually go inside one of these other functions and just copy paste the query and execute uh so this blocker code right here paste it inside our function and then we’re just going to go and change the statement from a select statement to a insert statement so we can say insert into users we’re going to include the column name so we have a username we also have a password and we have a email then we’re going to say values which is going to be what is inside these parentheses so again we’re going to use placeholders so this is going to be username and we’re going to include a password and we’re also going to include a email and then we just need to bind the parameters down here so we can actually copy paste in the username we can copy this down two more times and paste in the password we can paste into email and this is all we have to do but now we did also talk about hashing in the previous episode because we need to make sure that when we paste in this password that it can’t be read from inside the database so right before we bind the parameters I’m going to create a variable I’m going to call this one options because I want to include a couple of options inside an array and this is going to contain a cost factor which is going to be called cost and we’re going to set this one equal to 12 and this essentially is just going to make it cost quite a bit more in order to actually run this hash and this is going to prevent the Hackers from actually brute forcing the way into your website so this is important uh then we’re going to go below here and actually run a hash so we’re going to say we have something called hashed password and I’m going to set this one equal to a function called password uncore hash parenthesis semicolon and then we need to tell what exactly we’re trying to Hash here so in this case you want to Hash the password that was submitted by user and I’m going to run a hessing algorithm called password undor bpts and I’m also going to go and pass in my options which is the variable we created up there and this is basically going to Hash our password using the bcrypt algorithm and also go and add a cost factor to it so it’s going to slow down the The Brute forcing process if someone were to try and hack their way into your website um so what we can do here is we can actually take our H password and replace that with our password then where we actually B our parameters because now we’re storing the H password and not the real password inside the database we do also need to make sure inside this array here that we don’t set it equal but we actually assign 12 to our cost just so we don’t get that error message there uh so with this we now actually have this function working so we can go back inside our uh sign up the PSP file here and we need to make sure we actually paste in these parameters so we could just go back inside our controller copy all these different parameters here go back inside our sign up the PHP and paste those in of course without all the different uh type declarations that we have here so after running this function we just signed up the user inside our website so what we can do is we can go down a couple of lines and then we just run a header function and a die function in order to actually stop the rest of the script from running just in case and we’re going to send the user back to the front page where we’re actually going to include a sign up success message so we can actually go and say we have a question mark sign up equal to success and before we end off the script here so before the die function I do actually want to close off my connection and my statements so I’m going to say we have our variable PDO I’m going to set it equal to null then I’m going to copy paste this down and I’m going to say I want to set my statement equal to null and in this sort of way we now have a basic signup system working inside our website so now what we could do is we could actually go inside our website to test this out but I do want to have our signup success message if we do actually do so uh so we’re going to go and use this little um URL parameter here in order to do that so right now we have a signup equal to success displayed inside our URL if this is actually successful uh so what I can do is I can go back inside our view function because this is the one that actually has the error messages and I can write a else if condition and then check if we have that inside the URL with the sign up equal to success uh before we do that let’s go and paste in what we actually want to display inside the website so in my case I do want to again just run a break but I’m also going to go below here and Echo out a paragraph that is going to say sign up success inside the condition I’m going to run a iset function that is going to check if we have a certain get method inside the URL so in our case here we are checking for a dollar signore get because that is something we can do whenever we have any sort of data displayed inside the URL for example sign up equal to success so I’m going to check for something called sign up which is going to be the key inside the URL so we have sign up equal to success so key equal to Value okay just like inside arrays and I’m also going to go and check if it is equal to a certain message so I can go right after our is set function here do make sure you do it after the parentheses so in between these two parentheses at the end here and I’m going to say and is my get signup equal to a certain string so in this case success and having done this we need to do one more thing that I actually forgot to do inside our sign up the link to PHP file which is to go down and inside our errors if condition I do need to make sure we exit this script if we do have any sort of Errors inside our code otherwise it is still going to continue running all this code down here even if we do get a error message so we do need to copy this die function here and paste it right below our header because then we do actually exit the scripts and with this we now have a complete signup system or at least a signup system with a error message system so you can actually see any sort of Errors you might have inside your signup form so if were to go back inside our web page I can actually refresh everything just to make sure everything is reset and going inside and typing in a sort of error so if I were to type something wrong uh let’s say I did not fill in my password and I wrote a invalid email if it would to sign up then you can see we get two error messages down below we get fill in all fields and invalid email used so as you can see we have error messages showing and telling us what we did wrong inside the signup form uh what we can also do here is we can actually sign up inside the web page because that is something we have set up as well so if we were to go in here and say I want to sign up as Danny Crossing and I want to type in one 12 three as my password and say I want to type in a email so I can say Crossing at gmail.com which is not a real email that I have but just to come with some kind of example here uh if want to click sign up now you can see sign up success and then if I were to go inside our database we now have a user inside our users table so at this point here the signup system is working but there’s one more thing we need to include inside in order for this to be a complete signup system at least I think this is a feature that is something you should have inside a signup system which is to make sure that if you create any sort of error messages inside the signup form let’s say I went inside my signup form and I wrote in a valid username and I did not type in a password and I did type in a valid email so let’s say we say example at gmail.com um at this point here if I were to get a error message when I try to sign up because I did not fill in my password it should send back the data that I already typed in inside this form so don’t have to retype everything if I get sent back with an error message because that is just really annoying so this is more of a usability feature for people to not have to retype everything inside this form here so in order to create this I’m going to go back inside my code and the first thing we have to do is make sure we actually send the data that the User submitted back to our signup page so we can actually show it inside the inputs and the second thing we have to do is actually show it inside the input so we have to do two things in order to get this working here so the first thing we’re going to do is I’m going to go back inside my errors if statement here and I’m actually going to create an array that is going to contain all the data submitted by the user so we can send it back to our index page so if we have some sort of Errors then I want to go in here before the header function by the way and create a array and in this case I’m going to call this one sign up data and we’re going to set this one equal to an array so we’re going to say brackets and semicolon and then opening this up we can now add some data inside our array here and we’re going to do that as a associative array so we want to have a key and we want to have a value for that key so the first key I’m going to have in here is going to be the user name because I want to send back the username and again this is a name we can come up with ourselves so you can call this whatever you want uh in my case here I do think username makes sense because this is the username then I’m going to assign a piece of data which is going to be the username the user actually submitted when it tried to sign up inside our form which is going to be variable username and the reason I know that is because if I were to scroll up here I can actually show you that we want to grab all these different data and send that back inside the signup form or at least all the data except for the password because I do think the password is something you have to retype every single time in case you get something wrong inside your input and that is just kind of something you see inside websites typically so we’re not going to send back the password but any other kind of data we are going to send back inside the index page so going back down I’m going to assign the second piece of data which is going to be our email or at least the users’s email they tried to sign up with so this is going to be called email and we’re going to copy that and paste it over inside our variable here so now we have the actual data inside an array and now we have to actually send it back by assigning it inside a session variable just like we did up here so I can actually copy this session variable that we have paste it down below and we’re just going to go and change the names in here so we can actually go and say sign upore data and then we can go ahead and say this is going to be equal to our signup data so with this here we can now send the data back to our sign up form so what I can do is I can go inside my index page and what we need to do now is just basically take all these inputs from inside our signup form and we’re going to replace it with a piece of PHP code that is going to check if we did actually have some data sent back to this form here because then I want to show a different version of these inputs here so the way I’m going to do this is I’m going to copy all my inputs here just so I have them and I’m going to delete them and then I’m going to go inside my sign upore view. in.php file since we now have to show something inside our page and like we talked about we have the model view on controller and the view is going to take care of showing something inside the web page so what I’ll do is I’ll create another function so I’m going to go below my declare here and create a function and I’m going to call this one sign upore inputs just to give it some kind of name then I’m going to go inside and I’m just going to paste in my HTML for now so now what we have to do is create a couple of conditions that are going to check if we have this data being sent back to aign up form because of an error message and if that’s the case then I want to include that data inside my inputs here instead of having just the regular inputs with no data so what I need to do is go below here and create a if condition and inside this if condition I want to check if we have a certain session variable existing inside our web page which is going to be the one that has all the data in inside of it so going inside of here what I can do is I can write a is set function which checks if a certain session variable is currently set inside the page so I can check for a session variable so dollar signore session and I’m going to check if we have something called sign upore data but now I do want to check for a specific piece of data inside this sign upore data array uh so what I can do is I can create my brackets afterwards here and refer to a certain P inside this array here so right now I want to check if we have something called a username so if we have this it means that we did not leave any input empty for this particular input so now we’re actually also checking if there was any empty input so we don’t actually have to check for that particular error message but we do need to check for another error message because what if I picked the username that already exists inside the website because if I did that it means that I need to retype the username because it is already taken so in that case I don’t want to type the username that used to submit it because it is wrong and it needs to be changed right so we also want to check for certain error messages in here the way we can do that is going afterwards make sure it’s after the first parenthesis over here and we’re going to write a and condition and we want to check for another is set so what I can do is I can copy paste this session that we have here paste it inside my is set statement and in this case we’re not checking for sign upore data but we’re checking for errors uncore signup and then inside our errors uncore sign up we have a specific error message that we want to check for so in this case here it is going to be username unor tagen taken taken username uncore taken but we do also need to keep in mind here that I want to show the data inside the input if this error message does not exist inside our website so I do need to write a exclamation mark in front of my iset function here so right now we’re checking if we do actually have the data available because the User submitted some data and if we do not have this error message inside the web page then I want to actually show the actual data the User submitted inside an input so we can actually copy paste what we have up here with the username and I can go down and say I want to Echo then I’m going to say single quotes in order to not mess up any HTML here because we are using double quotes inside the HTML and then I’m going to close it off here and now the way we actually show data inside an HTML input and again this is HTML this is not PHP knowledge is by actually going in and adding a value attribute and then I’m simply going to close off the HTML by using single quotes and then I’m going to concatenate some PHP code and I’m going to paste in my data from inside our session so I can actually go and copy that from up here inside our condition and paste that in inside our value then I want to run a else condition because if this is not the case and we did not have any sort of data sent back and we did maybe have an error message then I do actually want to go in and show the Reg input without any sort of value inside of it so I do need to Echo that out as well so I’m going to Echo out single quotes paste it in and semicolon and this is basically all we need to do for these different inputs here so now the second one is going to be our password and in this case here with the password we did already talk about that I did not send the password back because I do want the user to retype it no matter what happens uh so in this case we do actually just need to Echo out our input for the password so we can just go and copy paste a password input here single quotes and paste it in and semicolon but when it comes to the email input we do actually want to copy paste this if else condition up here paste it below and then I want to use my email instead so instead of checking for sign upore data username I want to check for a email I do also want to make sure we’re not checking for a error message that is called username unor taken but instead I want to check for a email used because that is the error message we had inside our sign up. in.php if we were to scroll up here you can see we have email used but now we do also have a second error Mage when it comes to our email which is actually called invalid email so we do need to check for that as well because if you have more than one error message for one particular input then that is important to check for as well so going back inside our view I’m going to copy paste from where we actually check for the ANS and all the way over to the first parentheses copy that and then paste it in right afterwards here because now we can actually check for a second error message do make sure you have all the parentheses correct and all the single quotes and double quotes because that will mess up something inside your web page if you do not have everything said correctly so with that in mind let’s go and change the second error message from email used to invalid email because that is the error message for that particular one so now all we have to do is make sure we replace the actual Echoes down here so I’m going to copy paste my email I’m going to go down below and paste it in instead of these Echoes that we have in here in both places by the way so both inside the if condition and inside the else condition and then we just need to make sure we also add in the value inside the first one up here so I’m just going to go up going to copy the value make sure you copy it exactly like I am here so after the double quote at the end there but also before the angle bracket so copy that go back down and paste it in right after the placeholder so space and paste in and then just go ahead and change the actual data inside of here from username to email go back up to the top here delete these HTML things that we just pasted in just for reference and now what we need to do is just copy paste our sign upore inputs function go inside our index page and actually spit this out inside our form so if we were to go inside the form here where we had the inputs before I can say I want to open up my PHP tag and close it again and just simply write out my function and in this sort of way we should now have our actual data being shown back inside the forms if I to type some sort of error message so if I were to go back inside and refresh everything inside my page here so if I go back inside my form and type a random username so just something jerus write something random inside the password and let’s go and create a invalid email so in this case here when I click the sign up button we should not have any sort of data inside the email field but we should have something inside our username field so we want to click sign up here you can see oh okay invalid email used we still have the data inside our username but it did actually remove the data from inside the email which is what is supposed to happen uh so if I want to go in here and say I want to type in a username that already exists inside the database so Denny Crossing type in a random password and then let’s go and create a valid email so let’s say example at gmail.com so if I would to actually submit this it should send me back without the username and without the password because the username is already taken right so if I were to click submit here you can see that we get sent back without the username without the password so as we can see this is working like intended so with all of that we can now go ahead and sign people up inside our web page and just to double check nothing is being inserted inside the database cuz that’s always a good idea you can see that nothing is getting inserted and with that we now have a signup system with both error messages and we have the data still being inside the forms if the user would submit something wrong you know so they don’t have to retype things the signup is actually working inside the signup system uh what I’m going to do here is I’m actually going to split up this video into two part since this is already pretty long and I don’t want you guys to sit here for like 3 hours or something I don’t know maybe this video is going to be 3 hours long who knows so we’re going to split this up into a signup part and then we’re going to have a login part in the next video so I hope you enjoyed this video and I’ll see you guys in the next [Music] one so in the last video we how to create a signup system inside our website just as an exercise to learn how to use all the different things we learned up until until now inside this course and we still need to create a login system since we you know now we have people signing up inside the website but we also need them to be able to log into the website uh so what we’re going to do to begin with is we’re going to start up inside our login. in.php file because this is the file that we need to start in in order to you know when the user goes inside our login form and submits the user name and password they send that information to this page so we can actually lock them inside our website from within this page here so in the same sense as we did inside our sign up the in to phsp file we’re just going to go ahead and check for a server request method just like last time we’re going to go in and make sure we do that to begin with so we’re going to open up our PHP tags and we’re simply going to check for a request method that is going to be a post method so if the user got to this page legitimately by actually submitting the form post just like with our signup system then we do want to run some code inside this file here so the first thing we’re going to do is actually grab the data so in the same sense as we did in the last one you can see there’s a lot of copy pasting because we did the same things already uh we’re going to go and grab the username and password from within our login form and just to show you if I were to go back inside my index page I do have a login form and I do have a signup form and inside my login form I do have two inputs one for the username and one for the password so not anything for I think a email which we had in the signup system because we don’t need to log in using a email so going back inside our login. in.php file I do also want to make sure that if the user actually got to this page illegitimately so if they did not submit a request method called post then I do want to go down and write a else statement again just like inside our sign up form so we were to go in here scroll down to the bottom you can see that we have a else statement that simply sends them back to the front page and kills off this script here so if we were to go back inside our log in you can see that we just simply paste that in and that is all we need for now at least and then we have a basic structure of you know sending the user back if they did not get here properly so what I want to do now is I want to go below our username and password that we grabbed using the post methods and I want to go in and do a dry catch block just like again in the last episode so if we were to go in here you can see that we do have a try catch block where we go in and we try some code and if it fails then we do want to catch an exception and actually you know write whatever error that might has happened inside the website so we know that an error has happened so again we’re just going to go and copy the catch block at the end here and we’re going to go back in and we’re going to replace the catch block that I autogenerated using the tap functionality we have inside editors typically so I’m just going to go and replace that and now we do have the ability to catch a PDO exception if something were to go wrong so now all we have to do inside this Tri block up here is actually run the code that is going to allow for us to check for error handlers and check you know if the user should be logged into the website if there were no error messages then of course you know we do want to lock them in so what we’re going to do to begin with is first of all I do want to make sure that I create a login controller a lockin model and a lockin view just like we did in the last episode when it came to the sign up because we do need to go in and create these different functions that has something to do with different tasks again we’re going to try and implement this MBC model system uh so you can get a little bit familiar with it when you get into object Orient PHP which I did just upload a crash course on object oriented PHP and I will include that inside this playlist here as the next episode because I do think it’s a very good idea for you to know about object oriented phsp so we’re going to get into objectoriented PHP after this video here and I just want to say don’t don’t freak out because a lot of people do think objectoriented PHP is very difficult but it’s really not you just need to have someone explain it to you in a beginner friendly sort of way so it’s really not difficult to learn object oriented phsp it’s just you need to have it explained properly and that way you can get eased into it and then you’ll you’ll realize that oh okay so this is actually pretty easy um so we’re going to do that in the next episode so please watch that one even though it’s not labeled number 30 or something inside this course here because it is relevant to what we’re going to do in the future inside this course here but for now let’s go ahead and create these different controller model and Views inside our files here so what I’m going to do is I’m going to go inside my includes folder and I’m going to create a new file I’m going to call this one loginor controller or c n. inc. PHP we just going to copy everything here and I’m going to create another file and I’m going to name this one the same thing except for controller we’re going to say model then I’m going to do the same thing but this time we’re going to call it view so linore view. in.php and we’re going to go and link to these files we just created because we do need to use some of the functions inside these files so we do need to have them available to us inside what we have here so what I’m going to do is I’m going to say I want to require underscore ones and I want to require these files one at a time time so the first one is going to be login actually just copy paste here cuz I did actually copy it I’m going to copy this down two more times because we do actually need to get the database connection first that is the first one we need to grab so we’re going to say dbh in.php which is the database connection that we created over here which is just a very basic file that connects to the database and then creates a PDO object which I do explain in that Next Episode by the way because we do go over objects in the next episode and then we can use this PD connection object in order to connect to our database again this is something we learned in the last episode but since I recorded that a while back I thought why not just mention it again so going inside our login. in phsp file we do need to make sure we have this connection and then we’re going to go down and say we want to grab the model and then we want to grab the controller and the order here does matter so you do need to have the connection first then the model and then the controller so now taking a look at what we did in the last episode you can actually see the we immediately started doing error handlers inside this script here and we’re going to do the same thing when it comes to login system so instead of doing things a little bit out of order like we did in the last episode because that might be easier for you to understand now you do know exactly what these error handlers do while we create this empty errors array why we do these if conditions down here why we do need to want to have this config underscore session why we do need to check for if we had any sort of Errors inside the array because then we need to send the user back with an error message so we do do have some things in here that we’re going to repeat inside the other file so we’re going to go and copy everything from when we actually check if the array that is called errors have any sort of Errors inside of it all the way up till the comment where I wrote error handlers then we’re going to go back inside the login file go down and simply paste that in so right now we are creating an empty array called Aros which is going to contain any sort of Errors you might be getting inside the website and in this case because we’re doing a login system and not a signup system of course this are going to change a little bit because you might not want to check for as many things for example why do we check for an email when it’s a login system we don’t have any emails passed from the user so we don’t actually need to have that uh so we don’t need to have all of this inside our code for now we’re just going to go Ahad and delete all of them except for the first one which is going to check for any sort of empty inputs and then we do actually need to go after this one and we do need to actually query the database because we need to actually grab the user from the database that they typed in using the username and we need to check if the passwords match because if this password the user just submitted matches with the user’s password from within the database then we do need to check for that as well and of course there’s many different error handlers you could be doing when it comes to a login system I think for now just because this is a basic example it is just enough just to check for empty inputs and check if the username exists inside the database so does the user actually exist inside our database and if the user exists does the password actually match inside the database as well so we have a couple of things we need to check for here so what I’ll do is I’ll go inside my loginor model file and I’m actually going to quy the database using the username provided by the user to see if the user exists inside the database and if they do then I do want to grab the password and check that up against the password the User submitted just now so what we’re going to do is we’re going to open up our PHP tags just like any other time we go inside a new file and inside of this one I’m going to create a function that simply goes in and gets the user so we can say getor user and I’m also going to go and declare strict types because we did actually talk about that in the last episode so we’re going to declare that we want to use strict types so we’re going to say parentheses strict underscore types and I’m going to set this one equal to one this means that once we do actually get inside this function down here we can also tell it that we want to require a certain data type and not just the variable but the variable has to be a certain data type so inside the parenthesis I want to get the connection so we want to grab the variable PDO which is the one we have inside our database connection here so we can actually connect to the database in order to query it the second thing we’re going to pass in is going to be the username because the username was given to us by the user so if I were to go back inside my login. in.php file you can see that we did actually get it up here so just simply going to quy the database using this username now now we did talk about strict types so what I can do is I can actually say this is a object type and then the second one over here is going to be a string type again we talked about the PDO variable being a object that we created inside our connection so whenever you use PDO in order to connect to the database in this sort of way we do do that by creating a connection object using PDO and we do that using this new keyword so we instantiate a new object that is from the PDO class again we’re going to talk about this in the next episode uh but this one is going to be our connection so we do want to make sure we grab that and refer that to as an object because that is what it is and then we just have the username being a string because that is what the user gave us so now inside this get user we simply going to go in and Cory the database and we can actually go and copy paste because inside our sign upore model we do actually have very similar function so we can actually go in and just copy everything from inside just this first one up here called get username just to grab one of them and we’re going to go back inside the model and simply paste that in now everything is going to be the same except for our select statement up here because right now we want to not just select the username but we do want to select everything from inside this table here so we’re going to grab everything from the users table where a certain username is equal to username so in the same way as we did before then we create a prepared statement we go in and bind the parameters to the placeholder so right now we have this name parameter up here so we just bind the username the user gave us and then we execute it and then we grab all the results from inside our database or at least we’re grabbing one result from inside the database because we’re not fetching all of the data but just fetch which is just one piece of data or one row from inside the database so with this basic function here we can now just go in and cor the database using a particular username which means that we can actually go in and grab our loginor controller and do a couple of other functions in order to check if the username exists inside the database and we can also do want to check if the passwords match inside the database because you know the username has to use the right password so I’m going to go inside my model again and just copy everything go back inside my controller paste it in and just delete everything inside the function and change the function name to something else because in this case here we might call this one is underscore username wrong so underscore wrong and inside the parameters here we’re just simply going to check for variable results which was the one that we returned inside our model here if we do actually have a result but there is a small catch here because if I qu the database and we do actually have a user inside the database with that username then I’m going to get this one as an array however if we do not have a user inside the database with that username then this is going to return as a Boolean so no longer as an array but as a Boolean because it’s going to be f because there’s no user inside the database so how do we do that when it comes to strict types because if we would to go back here and say okay well I want to require variable result to be of a certain data type because if I write array then if I don’t have the user inside the database then we’re going to get an error message because now the strict type is wrong so if we were to type bull instead which is a Boolean then now it’s going to be wrong if we actually do get a result from inside the database so the solution here here is that we can go in and write this pipe symbol and say we also want to accept an array so now we’re saying that if this is either a Boolean or an array then we do want to accept the results so this is how you can combine different data types and say that you want to accept one just one type of data so not just a string but also a string and a Boolean if you wanted to uh so just to show you that this is how we can do that inside the function itself we’re going to write a if condition I’m just going to copy paste for my code here because it is very simple so going in we’re just going to create a if condition and we’re going to check if result is equal to false then return true because if it returns as false it means that we did not find the user inside the database because we don’t have any data inside this array here otherwise return this as false which means that we did not have any sort of error messages I know that the true false May logically seem like it switched around but it’s not really because the function is called isue username wrong and if this one is true it means that it is wrong and this is how we can simply check if the username exists inside the database and I know we haven’t actually use the function inside our login theing the PHP file yet but it will make sense in just a second so what I’ll do is I’ll copy paste this function below here and I’m going to check is password wrong because we do also need to check if the passwords do actually match because we do have a password from the user that just tried to log in and we do have a password inside the database which by the way has been hashed so we need to actually check these two against each other um and then we need to return this as true if they do not match each other or false if they did match each other the simple way to do this is just to go inside our parameters and say we want to get a string which is going to be the password submitted by the user and then we also do want to get another string which is going to be the has password from inside our database so PWD so grabbing these two different passwords we can go inside the if condition and simply run a password underscore verify and then we’re just simply going to check these two different pieces of data up against each other so we can check if password and H password do actually match each other but we do need to keep an eye out here because we are actually right now checking if the passwords do actually match each other which if they do then we’re actually returning a error message and that is the opposite opposite of what we want to do so we do need to make sure we go inside the if condition and do a exclamation mark in front of our password verify because now we’re checking if this is equal to false but now we do also have one last one we need to create because going back inside our login. in. PSP file we do have one more error Handler that we haven’t actually addressed yet which is right now is input empty because right now this is actually the one that matches up with the signup form and not the lockin form because as you can see we have an Emil in here so we can’t just reuse it and it is also just better to separate functionality so you know all the code for the login system in one place and then all the code for the signup system in another place because that kind of goes into the NVC model pattern that we’re going to talk about in a future episode okay so we do need to create a function for checking is input empty inside our linore controller so that is important so going inside the loginor controller I’m just going to copy paste the function here so you can just copy paste it as well uh it’s very basic we just go in and create a function and we call it is input empty and then we just submit a username and a password not an email this time around and we just go inside our function and just do a if condition to check if username and password are empty so with these functions here we now have a very basic example of checking for different types of errors that might occur inside our login form again if you want to add more you can do that uh for this tutorial here we’re just going to do these three for now so going back inside the login form we can actually make some adjustments so right now we do actually have this is input empty but I do want to make sure we delete the email and again this function here is something that we can actually grab because it’s inside our login controller which we did require right above it so we do have access to this function and then below here we do actually want to run our model function because we do want to actually grab any sort of data that might be inside the database where the username is equal to this particular username so what I can actually do here here is I can create a variable and I’m going to call this one result and I’m going to set it equal to getor user which is the function we have inside our model so inside our model file here where we actually go in and we qu the database and grab a user if it does exist so we’re going to grab a user and we do also want to make sure we submit our database connection as well as our username so we can actually go and say we have the username up here which was the second parameter by the way so we do need to go in front and also grab our PDO connection again the PDO connection is inside our database file which is linked up here so we can actually grab this one directly and just like that we now have a variable that has the results from the database query where we go in and actually try to grab the user so now we can use this in order to complete our next two error handlers which we did create inside our controller here so we do want to check if the username is wrong and we do also want to check if our password is wrong so after grabbing the data we can run a simple if condition just like up here when it came to the empty inputs and we can check if our username is wrong by simply running this function that we just created inside our controller and then we pass in our results that we just created in order to check if we did have any results from the database if we did not then we want to return a error called loginor Incorrect and incorrect login info and all this does is that it creates a error message that we can grab once we do actually get back to the login form if there was any sort of error messages and then we can display those errors inside the login form so the next one I’m going to check for here is if the passwords actually do match and again we did create a function for checking that so what I can do here is I can go below and I can run another if condition but this time I do want to go in and check if the username is not wrong so if it is actually a user that exists inside the database but also if the password is incorrect and you’re going to notice inside our is password wrong we just pass in our password which was submitted by the user right up here and we also pass in the password from inside our database query so the one we grabbed inside variable result which has a column name as password so with these error handlers here we basically just go in and check if the username exists and if you do actually have the passwords matching inside our database so with that we can now go back down inside our if condition that checks for any sort of Errors inside the errors array and I’m actually going to go and delete our session variable called sign upore data and also the actual array called sign up data and the reason for that is I do think that when a person types something incorrect inside a login form I don’t think that we should retype what they submitted into the login form I think they have to start completely over with the username and a password so it’s a little bit different than the signup form where if they wrote One Thing incorrect but the email was correct and everything else then we just you know we don’t want them to retype everything again cuz a sign up phone can be lengthy but in this case here I do think that that they should just type everything again and again just to go over what exactly this code does because you may not remember it from the last episode The require once where you grab the config unor session basically just goes in and grabs our session config file which goes in and updates the session ID once in a while inside the cookie inside your browser session security thing so to speak so inside our config session you can see that we you know we set out some parameters and you know we update the cookie every once in a while every 30 minutes so um so so now what we need to do here is a security thing which is that whenever you have any sort of changes happening when you lck in a user inside a website or something else that you know maybe some uh roles have changed inside the the website what you want to do is you want to make sure you always update the session ID cookie again when you make these changes so when you log a user into the website then update the session ID cookie because that is just a good habit to have so you don’t have to wait every 30 minutes you know from inside the config file just do it whenever something changes inside the website but now we’re going to do this in a slightly different way because if I were to go back inside my config file you can see that the way we did it before is by running a session regenerate ID which is how we can take the current session ID that is inside our website and we then regenerate it because if someone were to grab the session ID and stole it from us then we can regenerate it to change it so now it’s no longer usable but something we can actually when it comes to a login system is we can actually grab our users ID and combine that with our session ID from inside our session and this is not something you have to do this is something we’re going to do together in this episode here just so I can show you how to do it uh but essentially there comes a couple of benefits to having the users ID inside the session ID for example because you can associate some data together with the actual user that is using a particular session ID um so there is some benefits to it and again I’m just going to show you how to do it and then you can determine whether or not you want to use it or not again it’s just an exercise to show you some different things so what we’re going to do is we’re going to go back inside our login. in phsp file and right below where we check for these error messages the next thing I want to do is I do want to create a new variable I’m going to call this one new session ID make sure we spell that correctly and I’m going to set it equal to a function that I do think I’ve mentioned before in the previous episode which is called called session uncore creatore ID so this is not a session uncore regenerate ID this is a session _ create ID which is a little bit different so now this will actually create a entirely new ID instead of just regenerating the existing ID we have current inside the website and what we can do then is we can actually append our users ID together with this newly created ID that we just created so if I were to go below here I can go ahead and say I want to create a variable called session ID and I’m going to set it equal to our new session ID that we just created up here and then I’m just simply going to append a ID that we got from our variable results a little bit further up inside our code because we did actually grab the users’s data from inside the database so if we were to grab the variable results go down I can actually append the ID of the user together with our session ID so again we’re creating a new session ID which is just for security just like we regenerate a new session ID we now just create a new one and then we just add our users ID together with our session ID using a underscore just so we can separate those uh the ID from the users ID and now all we have to do is just go below here and say we want to run a session ID function which goes in and actually sets our session ID equal to a ID that we give it inside the parentheses here so this case we can go in and tell it to set our session ID equal to what we just created up here with the users ID so again a small trick here just to you know create a new session ID inside our website using also the users’s ID from the user we tried to log in inside our database here and there is something we have to do here as well in regards to this because right now we do have our users ID together with our session ID but what happens in 30 minutes when I go inside the config session code and it automatically updates the session ID in 30 minutes well when it does that we no longer have the users ID part of our session ID because it is just regenerating the the session ID without appending the users’s ID in 30 minutes so that is something we have to change inside our config file so what we need to do here is we do actually need to check if the user is currently logged in or if they’re not logged into the website because if they are logged into the website side then I do want to make sure that when we regenerate the session ID that we do so using the user’s current ID so inside our if condition at the bottom here where we go in and update this every 30 minutes I do want to create another if condition so again now we’re going to start to have some nesting going on but I think it’s okay for now and I’m simply going to go inside my parentheses and check if we currently have a user locked into the website by checking if we have a current session variable that is equal to user on the _ ID or at least that is named user uncore ID so by going in here we can say we have a variable underscore session which is a super Global we have talked about this one plenty of times by now so we go in and we check if we have a user ID which we haven’t created yet by the way this is something we will create once we actually log the user in instead of login.in phsp file just know for now that we will have a session variable called user ID when the user is logged in okay then I’m going to create a else condition so we’re going to say else and inside the else condition we want to run the code that is going to get run when the user is not logged into the website so that would actually be the code that we have here already so it would to copy paste that inside my else condition this is now getting run if the user is not logged into the website but what if the user is logged into the website because now we need to change things a little bit here all we have to do is we need to go inside the if condition paste in the same code again but instead of running I’ll regenerate on _ session ID which is a function that we created down here we’re going to create a new function that is going to go in and append the users’s ID once you do actually regenerate the ID inside once we’re logged into the website so all we need to do here is create a new function called regenerate unor session idore loged in and the same thing down here for regenerate uncore session ID logged in then we scroll down inside our function here and just copy paste it because we do need to have another function for when we’re logged in so I’m going to change the name of one of these to logged in and we’re going to keep the same code as we have in here because we do still need to use this code uh we do need to make sure we write true inside these parentheses here though inside both functions because we forgot to do that in the last episode so after regenerating the ID we’re going to do the exact same thing as we did inside our login that in the PHP file so I’m just going to go in I’m going to go down here where we actually generated the ID using our ID from the user so I’m going to copy that go back in paste it in but we do need to change one thing here which is that right now we don’t have a result called ID because that is specific to that particular file in there uh but we do have a session variable that does exist called user ID if the user is logged in so we can actually take that one go down here create a new variable we can call this one user ID and set it equal to our session variable called user ID and then we can use that ID instead of our result from the database because like I said it’s inside that other file so we can’t access this variable specifically uh but we can access a user ID from our session variable if we’re logged into the website so again just to recap here we’re going in regenerating the ID which we technically don’t have to we could delete this if we wanted to we’re grabbing the user ID from our session variable if we’re logged in then we’re going down and creating a new session ID by using this session create ID then we’re going in and saying that we want to set a session ID equal to the new session ID we just created and the user ID from the user and then we’re just simply going in and saying that we want to set the session ID equal to this session ID up here and then we update the time for when we last regenerated the session ID because now it needs to wait 30 minutes until it will automatically go in and again regenerate this using the users ID if we’re logged in this may be the most confusing part of this video by the way again just know that we’re going inside the website and updating the session ID by adding the users ID with it as well to create some faint new things we can do potentially if we wanted to do that in the future uh we’re not going to do anything with it in this video here it’s just to show you that you can do it so inside our login. in.php file at this point here we do actually have a user that tried to sign up with a username that exists inside the database and also a password that matches with the password inside the database so what we can do is we can actually sign up the user inside our website the way we’re going to do that is first of all by setting the appropriate session variables so we’re going to say we have a session variable and we’re going to call this one user uncore ID which by the way inside the config uncore session we are checking for if the user locked in into the website and we’re going to set this one equal to the ID from inside our database so we can actually go ahead ahead and set this one equal to our result from our database quy and the ID column from inside the database which is the ID of this particular user here so I can copy paste this down and then we might also include maybe the username so the user undor username just so we have it available to us so we don’t have to quote the database every single time if I just want to graph the username of the user uh so with this we can set it equal to username because that is the name of our database column but now I do want to do something else here because we might actually take this username and print it out in our website somewhere we might try to Echo it out because I want to show the user who they’re logged in as right now inside the website uh so we do need to make sure we do some security so we’re going to use HTML special characters and make sure we run a sanitation of variable result just to make sure everything has been sanitized to avoid any sort of potential crossy scripting that might be happening inside the website here and with that I do want to do one more thing which is to go in and actually reset the timer for when we have to update our session ID because we just did so so why not go in and just reset the timer for that so I’m going to go inside my config unor session and I’m just simply going to go down and grab our session variable called last regeneration which is equal to time copy that entire lineer code go back in and right below here where we actually to create these session variables I’m just going to go and create that one as well by setting it equal to the current time so now we reset the time so that in 30 minutes it’s going to update itself again and just like that we now have a login system so we can actually go below here create a header function to send the user back to the front page with a login equal to success message uh we can also go ahead and close off these statements and our connection because the prepared statement and the connection it does get automatically closed but it is just a good habit best practice so to speak to actually close it manually inside your code uh so we’re going to go and do that as well well so right below the header function I’m just going to say we want to close off the PDO and the prepared statement and the last thing we’re going to do is to create a die function essentially just terminating the script at this point here so now we have a login system that should be working inside our website but let’s actually go back inside our index page and make sure if we have any sort of error messages related to the login form for example submitting the wrong password or a username that doesn’t exist inside the database then we want to Output that error message inside our form or at least below the form inside our front page so going below the login form I’m going to go down and open up my PHP tags and inside here we’re going to refer to a function called check linore errors and this is a function we haven’t created yet because this is going to actually output something inside the website which means that it needs to go inside our view so going inside our loginor view you can see that right now we have nothing so what I’m going to do to begin with here is open up the PHP tags and open up the strict mode and all that U so I’m going to go inside one of the other ones that I have here and just kind of open up what we have here you know so we have the beginning of the PHP tag we have the declare strict types and then we’re going to create the function that we just had inside our index page I’m just going to go and drag this over so we can actually see it and I’m going to copy the function name inside the index page go back inside my loginor view and say I want to create a function that has a name set to check unor linore for errors so inside this function we’re going to run a if condition and I want to check if we have a certain session variable set currently inside the website so if we have a is set parenthesis and we have a session variable that is called error uncore login so errors uncore login and this is something we actually do need to change inside our login. in PSP file so if we would to copy the name here go inside the login the PHP and scroll up to our error messages you can see that right now we’re creating a session variable called errors unor signup because we copy pasted this from our signup form so we do need to make sure we’re go and change that to errors unor login and then if we have any sort of error messages then I want to go inside this if condition and simply create a variable called erros and set it equal to the session variable that has all the errow messages inside of it so we’re just going to set equal to variable uncore session errors uncore lugin because by doing that we can now go below here and we can actually unset the session variable because remember if there’s any sort of information inside our session variables that is no longer needed then we need to make sure we unset it you know to clean it out so in between here I’m just going to go and Echo out a break just to get some distance between our form and the error message that we’re going to print out and then I’m going to run a for each Loop because we do actually want to go in and make sure that we take all the error messages inside our variable errors and print print them out below each other inside our login form or below the login form just like we did when it came to the signup form so inside my for each Loop I’m simply going to Echo out a paragraph that has a class set inside of it which I did style inside my stylesheet and I’m just simply going to print out a error so I’m going to go inside our parameters up here for the for each Loop and I’m going to say that I have a errors array and I want to refer to error whenever I refer to one of these errors inside the array and then because I refer to to error in between the paragraph down here we just simply spit out the error that we might be getting inside our form so with that we now just need to create a success message if we did actually not have any sort of Errors so going below here I can create a else if and I’m just simply going to check if we had any sort of success message inside the URL when we did send the user back with a you know lockin success message so we’re going to use a get method in order to grab a login and we’re going to check if it is equal to success and and again just to show you where this happens if we were to go back inside our login. in. PSP file at the bottom here when we do access send the user back you can see that inside the URL we have lugin equal to success and that is what we’re checking for here and now we need to do one more thing before we can actually test this out because right now inside the index page we are referring to a check unor linore errors which is inside our view file uh but we did not actually link to our view file at the top here so we need to go up here and say we have a lock inore view. in the PHP file so now we do actually have access to this function in here so if we were to go inside our website and let’s go ahead and sign up as a new user because I don’t actually remember the password from the user we created in last episode so let’s just go ahead and go in here and say we have John do and we’re going to say we have a password that is one 12 three which is most likely what I used in the last episode as well so John at gmail.com I’m just going to go and sign up this new user and then you can see oh sign up success and now we can actually go inside our login form and we can test this out so if we were to say John do with capitalized d and capitalized J and then one two three log in then you can see we get login success if I were to try and go in and actually log in with something that does not exist inside the database let’s actually go and leave this one empty you can see oh fill in all Fields incorrect login info uh if we were to pick something in both Fields then you can see we get incorrect login info because saf does not exist inside our database as a user if I were to use John Doe John do but use a incorrect password then you can see we get incorrect login info because this was not the correct password so everything is working exactly like intended but now a question that people may have is okay so we have this login system but how do we change content inside the website once we’re logged in cuz that is important to do too before we do that let’s actually go and create a log out button because I do want to be able to lock out the user as well because right now we’re just logging in as one user and then trying to lock in as another user but we’re not actually locking out at any point so going inside the index page here I’m going to create a new form I’m just going to call this one log out so I’m going to copy paste this one and let’s go ahead and put this one at the bottom so we’re going to say log out like so and we’re going to refer to a log out. in the PHP file and we’re not going to have any sort of inputs in here we’re just going to have a logout button then I’m going to go in and actually create a new file inside my includes folder so I’m going to say new file call it log out. in.php and inside this file here we’re just going to open up our PHP tags go down and say we want to create a session uncore start because we do want to start a session in order to actually destroy it and then below we’re going to go and say session uncore unset and then we’re going to say session uncore destroy and then we can actually send the user back to the front page so we can actually copy paste that from let’s say I’ll login. in.php file so we want to go down to the bottom here we have a header that sends the user back and we have a die uh function so we can copy those and paste those in so if I were to go inside my website refresh everything you can see we have a log out button so if we were to actually log in using John do one two 3 log in then you can see we are logged in as John do well you can’t actually see it cuz you can’t see if we’re logged in or not uh but we do get a login success message and then if I want to I can loog out down here and then I will be locked out again so now the question is how do we change content inside the website when we’re locked in when we’re not logged in you know because we need to change content because that’s the whole point right we create a login system so we can log a user in and then change content inside the website so they might have a profile page or something um so the way we can do that is if I were to go back inside our view for the login system is I’m actually going to go and create a function to print out the username when we’re logged in so we can actually see the username inside the website and it says you are logged in as and then the name of the username so I’m going to create a basic function I’m just going to copy paste it here which is called output username and then inside we have a if condition that simply goes in and checks or we currently logged in by checking if we have a user ID because if that exists that means we’re logged into to the website and if we’re logged in we’re just simply going to go in and output you are logged in as and then we’re going to grab the username from inside our session variable again that’s why we wanted to set that as a session variable when we did actually log in previously and if we’re not logged in we’re just simply going to say you are not logged in and then we can use this function we can copy it go inside our index page and just simply output it somewhere maybe above our login form so we can say we have a let’s create a H3 and we’re going to go and say we want to have a PHP tag opening and closing in here and then we’re just simply going to run this function here so anytime you want to change anything inside the website if you’re logged into the website again all you have to do is run a if condition that checks if currently we have a session variable that is called user ID and that is all you need to check for if that exist then change the content Again by echoing something out or creating some HTML or something um so if I were to go back inside the website you can now see that when I refresh it says you are not logged in cuzz we’re not logged in but if if we were to go down here and log in as John Doe 1 2 3 log in then you can see we get you are logged in as John Doe and again if I were to go down to the bottom here log out then you can see oh you are not logged in cuz we destroyed all the session data with the session variable and now we’re no longer logged into the website and we can do the same thing when it comes to the forms so if we were to go in here and say you know what let’s go inside our view and just copy this if condition go inside our index page and let’s say we only want to see the login form if we are currently not logged into the website because there’s no need to have a login form if we’re already logged in right so if we were to go in here we can go ahe and open up the PHP tags and close it off again we’re going to paste in the if condition here and then we can just go in and actually include the form inside the if condition for when we’re not logged in so what we can do here is we can say okay so if we are not currently logged in and instead of echoing out you are logged in as and then the username we can just go in and we can copy paste the form and paste it inside our condition here so I’m going to copy the form delete it go inside and I’m just going to go and close off my uh PHP tags here just because it makes a little bit easier so again opening and closing the PHP tags wrapped around the closing bracket and also opening and closing it wrapped around the first if condition line of code here because that allow for us to go in and paste in HTML and still have it be part of the PHP again this is PHP syntax good practices 101 so instead of you know putting this in as a string by echoing all of this out can just write HTML code directly Inside by simply opening and closing the closing bracket and the same thing for the opening bracket so if I were to go in here and do this then you can see that if I were to go inside the website refresh it right now we’re not logged into the website so we can see the form but if we were to go in and log in as John do 1 2 3 then you can see that oh okay the login form disappeared cuz we no longer need to see it and that is how you can simply go inside a website and change content based on if you’re logged in or if you’re not logged in and you can do the same thing for the sign up form that could also disappear by the way and the same thing for the log out form maybe that should not be there when you’re not logged into the website because it doesn’t make any sort of sense so with that I hope you enjoyed this little episode on how to create a login system and I’ll see you guys in the next video [Music]

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog