<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mateusz Jasiński </title>
    <description>The latest articles on DEV Community by Mateusz Jasiński  (@wizarddos).</description>
    <link>https://dev.to/wizarddos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F887953%2F17ee42de-3a3c-4c38-b8e9-2952808b5627.jpeg</url>
      <title>DEV Community: Mateusz Jasiński </title>
      <link>https://dev.to/wizarddos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wizarddos"/>
    <language>en</language>
    <item>
      <title>Self-refreshing view counter in PHP and JS</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Sat, 13 Apr 2024 20:02:04 +0000</pubDate>
      <link>https://dev.to/wizarddos/self-refreshing-view-counter-in-php-and-js-58k8</link>
      <guid>https://dev.to/wizarddos/self-refreshing-view-counter-in-php-and-js-58k8</guid>
      <description>&lt;p&gt;Hi there, long time no see huh&lt;/p&gt;

&lt;p&gt;Have you ever written an admin panel for your website?&lt;/p&gt;

&lt;p&gt;There is one interesting statistic I personally like looking at - view count&lt;/p&gt;

&lt;p&gt;We're proud to see people enjoying themselves while using our product, aren't we?&lt;/p&gt;

&lt;p&gt;Then, we ought to know how many people do we attract&lt;br&gt;
Time to finish this intro what will we code today?&lt;/p&gt;
&lt;h2&gt;
  
  
  Project plan
&lt;/h2&gt;

&lt;p&gt;First - We'll create a bit of backend.&lt;br&gt;
We need&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database with table "views".&lt;/li&gt;
&lt;li&gt;Script for database connection.&lt;/li&gt;
&lt;li&gt;A script that passes &lt;code&gt;views&lt;/code&gt; content as JSON.&lt;/li&gt;
&lt;li&gt;Another function, that will update &lt;code&gt;views&lt;/code&gt; table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, time to hop into frontend with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI for the counter and admin-panel.&lt;/li&gt;
&lt;li&gt;Javascript parsing data function executed in an interval.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;Start your server and let's get to work!&lt;/p&gt;

&lt;p&gt;I've created a simple database called &lt;code&gt;view-counter&lt;/code&gt;. In it one table called &lt;code&gt;views&lt;/code&gt; with one &lt;code&gt;count&lt;/code&gt; field in it&lt;br&gt;
There, add one row with value &lt;code&gt;0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We need a function for executing queries. As our code will send a bit of requests, such function will definitely be useful&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;
    &lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"mysql:host=localhost;dbname=view-counter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rowCount&lt;/span&gt;&lt;span class="p"&gt;()){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"No result"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;FETCH_ASSOC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PDOException&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Error occured"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, replace strings passed in &lt;code&gt;PDO&lt;/code&gt; constructor with your DSN and credentials&lt;br&gt;
&lt;code&gt;$params&lt;/code&gt; was set to be equal to &lt;code&gt;[]&lt;/code&gt;, as there's no need to use it every time - it's an optional argument&lt;/p&gt;

&lt;p&gt;We'll need to access current view count 2 times - once to display and once to increment it&lt;/p&gt;

&lt;p&gt;Let's write a quick function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getViews&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM `views`"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s1"&gt;'count'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step: Add &lt;code&gt;pageVisited()&lt;/code&gt; - one to actually updates the counter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;pageVisited&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nv"&gt;$currentVisits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getViews&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$currentVisits&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"UPDATE `views` SET `count` = ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$currentVisits&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't need to return anything. &lt;br&gt;
I put all of these functions inside &lt;code&gt;lib.php&lt;/code&gt; in &lt;code&gt;scripts&lt;/code&gt; folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; 

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;
    &lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"mysql:host=localhost;dbname=view-counter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rowCount&lt;/span&gt;&lt;span class="p"&gt;()){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"No result"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;FETCH_ASSOC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PDOException&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Error occured"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getViews&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM `views`"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s1"&gt;'count'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;pageVisited&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nv"&gt;$currentVisits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM `views`"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s1"&gt;'count'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$currentVisits&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;runQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"UPDATE `views` SET `count` = ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$currentVisits&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition we need &lt;code&gt;api.php&lt;/code&gt;. &lt;br&gt;
Here, JSON will be returned for JavaScript to grab;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"scripts/lib.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getViews&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Backend, checked. Shall we jump to the front?&lt;/p&gt;

&lt;h2&gt;
  
  
  JS and HTML
&lt;/h2&gt;

&lt;p&gt;We need 2 pages and 1 JS file&lt;/p&gt;

&lt;p&gt;Pages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;page.php&lt;/code&gt; - Page where we count visits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;admin-panel.php&lt;/code&gt; - To display the counter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;update-counter.js&lt;/code&gt; - Will update the counter on &lt;code&gt;admin-panel.php&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For &lt;code&gt;page.php&lt;/code&gt;, we don't need anything big. Just remember to update the counter with each visit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Mysterious Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;This page is not totally tracked&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"scripts/lib.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;pageVisited&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's time to shine with &lt;code&gt;admin-panel.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Admin page - check your views&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page was visited &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id = &lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt; times&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src = &lt;/span&gt;&lt;span class="s"&gt;"scripts/update-counter.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've written simple HTML, but fun is about to begin&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;scripts&lt;/code&gt; folder add &lt;code&gt;update-counter.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We need a function, that will grab data from &lt;code&gt;api.php&lt;/code&gt; and replace the contents of &lt;code&gt;cound&lt;/code&gt; span&lt;/p&gt;

&lt;p&gt;I want it to be replaced every 5 seconds&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;updateCounter&lt;/code&gt;, we need to make a request and update &lt;code&gt;#counter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`http://localhost/view-count/api.php`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As it's here, first we get data from &lt;code&gt;api.php&lt;/code&gt; and treat it as JSON&lt;/p&gt;

&lt;p&gt;Then&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counterElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#count&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;counterElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Grab that data and replace &lt;code&gt;counterElement&lt;/code&gt; with updated number&lt;/p&gt;

&lt;p&gt;Overall, &lt;code&gt;update-counter.js&lt;/code&gt; looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`http://localhost/view-count/api.php`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counterElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#count&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;counterElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In browser, at &lt;code&gt;admin-panel.php&lt;/code&gt; we'll encounter.&lt;br&gt;
&lt;a href="https://postimg.cc/NL0PBZDB"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9cArGwi4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.postimg.cc/Kc7Xr2gk/obraz.png" alt="Counter on 0" width="320" height="53"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now head to &lt;code&gt;page.php&lt;/code&gt; - when you come back, even without refreshing on &lt;code&gt;admin-panel.php&lt;/code&gt; you'll see.&lt;br&gt;
&lt;a href="https://postimg.cc/BL7pjQps"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DDQbnqQK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.postimg.cc/tJpMmsV1/obraz.png" alt="Counter on 1" width="320" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! We made it&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thanks for reading this article.&lt;br&gt;
Now in addition to &lt;a href="//https:/wizarddos.github.io/blog/programming/php_0_to_hero/12"&gt;login&lt;/a&gt; and &lt;a href="//https:/wizarddos.github.io/blog/programming/2024/04/02/php-javascript-ajax-fetch-infinite-scroll.html"&gt;inifinite scroll&lt;/a&gt; you have view counter in your arsenal ready to use everywhere you need&lt;/p&gt;

&lt;p&gt;Hope you've enjoyed coding with me - check out 2 articles linked earlier and my &lt;a href="https://wizarddos.github.io/blog/series/php_0_to_hero.html"&gt;whole PHP course&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Code for you to download is available &lt;a href="https://github.com/wizarddos/Dev.to-scripts/tree/master/view-count"&gt;here on my Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it, see you next time.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>php</category>
    </item>
    <item>
      <title>Learn x86 Assembly with me</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Mon, 04 Mar 2024 18:58:47 +0000</pubDate>
      <link>https://dev.to/wizarddos/learn-x86-assembly-with-me-4hhg</link>
      <guid>https://dev.to/wizarddos/learn-x86-assembly-with-me-4hhg</guid>
      <description>&lt;p&gt;What's up.&lt;/p&gt;

&lt;p&gt;Based off of my posts, I think it might be kind of strange to see an article like this&lt;/p&gt;

&lt;p&gt;But yeah, I've decided to learn assembly&lt;/p&gt;

&lt;p&gt;Also - Correct me if I'm wrong somewhere here or oversimplified a term. I'd be more than happy to read such comments and get my knowledge and understanding to the higher level&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Assembly and why x86?
&lt;/h2&gt;

&lt;p&gt;Starting with the second part - that's what my processors architecture is. &lt;/p&gt;

&lt;p&gt;I don't have ARM processor, yet someone would tell me.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just buy used computer or VPS with such access&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I could, of course - Yet I would not like to spend too much money right now&lt;/p&gt;

&lt;p&gt;I believe, that with some curiosity and time to search, I can dig information for free - This will be the challenge itself&lt;/p&gt;

&lt;p&gt;I like challenges - so why not?&lt;/p&gt;

&lt;p&gt;And why assembly? Simple answer...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reverse Engineering&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Just got into it - so Assembly might be fairly useful there, as not always everything can be found with C# or even C de-compiler&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I'll be learning from?
&lt;/h2&gt;

&lt;p&gt;I've found 2 decent sources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.tutorialspoint.com/assembly_programming/index.htm"&gt;tutorialspoint&lt;/a&gt; - in english, with nice segments and everything&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ww2.ii.uj.edu.pl/~kapela/pn/print-lecture-and-sources.php"&gt;This paper from Jagiellonian University&lt;/a&gt; - yet in polish, so my native language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, that's it for the talking - let's learn something&lt;/p&gt;

&lt;h2&gt;
  
  
  What does assembly code consist of
&lt;/h2&gt;

&lt;p&gt;If we look at assembly code - it consists of &lt;strong&gt;sections&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some basic ones are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;.bss&lt;/code&gt; - In this section, we declare variables, that will be changed while execution&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.data&lt;/code&gt; - it stores constants, like strings or ints&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.text&lt;/code&gt; - contains the code itself. We need to also declare &lt;code&gt;_start&lt;/code&gt; here
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section.text
    global .start
_start:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;_start&lt;/code&gt; tells the kernel, where should it look for the beginning of the code.&lt;/p&gt;

&lt;p&gt;In there, we can find &lt;strong&gt;mnemonics&lt;/strong&gt; - Assembly instructions&lt;/p&gt;

&lt;p&gt;In one line, there can only be one mnemonic&lt;/p&gt;

&lt;p&gt;So, this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV TOTAL, 48 ADD TOTAL, 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can never appear - so we need to write this in 2 separate lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV TOTAL, 48 ; Move 48 to variable in memory called TOTAL
ADD TOTAL, 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, I've also added &lt;code&gt;;&lt;/code&gt; to the end - that's &lt;strong&gt;comment&lt;/strong&gt; in assembly&lt;/p&gt;

&lt;p&gt;That's it, not to much yet still I hope it will expand very soon&lt;/p&gt;

&lt;h2&gt;
  
  
  What will I learn now?
&lt;/h2&gt;

&lt;p&gt;Probably start reading about registers and other parts such as variables or conditional instructions&lt;/p&gt;

&lt;p&gt;Right now, I'd like to thank you for reading - Share you expirance in comments. Maybe we as community can learn a very valuable lessons from you.&lt;/p&gt;

&lt;p&gt;Also, feel free to share your sources - where have you been learning assembly from and that's it&lt;/p&gt;

&lt;p&gt;See you next time&lt;/p&gt;

</description>
      <category>assembly</category>
      <category>beginners</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.16 - .htaccess and robots.txt files</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Tue, 27 Feb 2024 16:31:56 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt16-htaccess-and-robotstxt-files-47b</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt16-htaccess-and-robotstxt-files-47b</guid>
      <description>&lt;p&gt;Welcome - Sadly or not, that's the last part of this series&lt;/p&gt;

&lt;p&gt;We finally come to an end - today I won't be talking about PHP itself&lt;/p&gt;

&lt;p&gt;You know, when we were writing a code, we've always had &lt;code&gt;.php&lt;/code&gt; at the end. Like &lt;code&gt;/login.php&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;But it can be better&lt;/p&gt;

&lt;p&gt;We want it more appealing, like &lt;code&gt;/login&lt;/code&gt;, don't we? &lt;br&gt;
That's the perfect example of &lt;strong&gt;&lt;code&gt;.htaccess&lt;/code&gt;&lt;/strong&gt; file usefulness&lt;/p&gt;

&lt;p&gt;Apart from that, I'll also explain usage and importance of &lt;code&gt;robots.txt&lt;/code&gt; file - by the way talking about it's security issues&lt;/p&gt;

&lt;p&gt;What will we learn today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When can we use &lt;code&gt;.htaccess&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;How to add SEO-friendly addresses to our website&lt;/li&gt;
&lt;li&gt;Adding error pages&lt;/li&gt;
&lt;li&gt;Basic &lt;code&gt;robots.txt&lt;/code&gt; structures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No more talking - let's go&lt;/p&gt;
&lt;h2&gt;
  
  
  When can we use &lt;code&gt;.htaccess&lt;/code&gt; files?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.htaccess&lt;/code&gt; is a config overwrite mechanism made for &lt;strong&gt;Apache HTTP server&lt;/strong&gt; - and only for it&lt;/p&gt;

&lt;p&gt;So, If you are using Nginx or Microsoft IIS - sorry to say, but you need to use native solutions.&lt;/p&gt;

&lt;p&gt;Right now (I assume) we are working with Apache server so using &lt;code&gt;.htaccess&lt;/code&gt; shouldn't be a problem for us&lt;/p&gt;

&lt;p&gt;Also&lt;/p&gt;
&lt;h3&gt;
  
  
  Quick disclaimer
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;.htaccess&lt;/code&gt; files, even despite it's popularity, comes with some flaws.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.htaccess&lt;/code&gt; slows down requests - It's server-side mechanism so It can't be cached in a browser. Every request requires "executing" &lt;code&gt;.htaccess&lt;/code&gt; file(s)&lt;/p&gt;

&lt;p&gt;99% of things we'll be doing in &lt;code&gt;.htaccess&lt;/code&gt; can be done in default Apache config file - &lt;code&gt;httpd.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have write access to it (like on VPS) - use it as much as you can. Your site will perform much better&lt;/p&gt;

&lt;p&gt;Also, &lt;code&gt;.htaccess&lt;/code&gt; is very vulnerable to syntax errors - one minor typo and you get error 500 all over your website&lt;/p&gt;

&lt;p&gt;So to sum up -  use &lt;code&gt;.htaccess&lt;/code&gt; files only if you don't have access to &lt;code&gt;httpd.conf&lt;/code&gt; (for example on shared hosting) and use only 1 at a time&lt;/p&gt;

&lt;p&gt;With this said - let's go&lt;/p&gt;
&lt;h2&gt;
  
  
  SEO-friendly addresses
&lt;/h2&gt;

&lt;p&gt;I think, while browsing the internet you've seen website with link like this &lt;code&gt;http://domain.com/home&lt;/code&gt; - That's where &lt;code&gt;.htaccess&lt;/code&gt; comes in handy&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;RewriteEngine&lt;/code&gt; we can create such addresses for pages&lt;/p&gt;

&lt;p&gt;First - enable &lt;code&gt;RewriteEngine&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;RewriteEngine&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we can start adding our rules&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;RewriteRule&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;.&lt;span class="n"&gt;html&lt;/span&gt;
&lt;span class="n"&gt;RewriteRule&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt;-&lt;span class="n"&gt;us&lt;/span&gt; &lt;span class="n"&gt;aboutus&lt;/span&gt;.&lt;span class="n"&gt;html&lt;/span&gt;
&lt;span class="n"&gt;RewriteRule&lt;/span&gt; &lt;span class="n"&gt;privacy&lt;/span&gt;-&lt;span class="n"&gt;policy&lt;/span&gt; &lt;span class="n"&gt;privacy&lt;/span&gt;-&lt;span class="n"&gt;policy&lt;/span&gt;.&lt;span class="n"&gt;html&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find some pattern - it looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RewriteRule [new address] [file]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When creating them, pay attention to letter case - write everything in lowercase and use &lt;code&gt;snake-case&lt;/code&gt; to separate words&lt;/p&gt;

&lt;p&gt;Why? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lowercase helps SEO - sometimes search engine might index &lt;code&gt;My-page&lt;/code&gt; 2 times. As &lt;code&gt;My-page&lt;/code&gt; and &lt;code&gt;my-page&lt;/code&gt; which makes you loose your position in search results&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;snake-case&lt;/code&gt; (with dashes instead of spaces) - When Google sees &lt;code&gt;my-page&lt;/code&gt; it replaces &lt;code&gt;-&lt;/code&gt; with space, as it can't distinguish any other meaning for it. Then it matches typical Google searches&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the other hand - using &lt;code&gt;_&lt;/code&gt; suggests that there is a letter, but we don't know which one. So &lt;code&gt;my_page&lt;/code&gt; could be read as &lt;code&gt;myopage&lt;/code&gt; or &lt;code&gt;myjpage&lt;/code&gt; and so on&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Documents
&lt;/h2&gt;

&lt;p&gt;From &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/8"&gt;part 8&lt;/a&gt; we know that sometimes websites don't work as we want - so we have &lt;code&gt;4xx&lt;/code&gt; and &lt;code&gt;5xx&lt;/code&gt; errors&lt;/p&gt;

&lt;p&gt;Now, displaying typical 404 Apache page looks unprofessional - some websites have their own error pages, with matching theme and some funny images&lt;/p&gt;

&lt;p&gt;We can do this in &lt;code&gt;.htaccess&lt;/code&gt; as well - for example with &lt;code&gt;404&lt;/code&gt; page&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;ErrorDocument&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;://&lt;span class="n"&gt;example&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;/&lt;span class="m"&gt;404&lt;/span&gt;.&lt;span class="n"&gt;php&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One question? Can't we just use relative path - &lt;code&gt;/404.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Not really - sometimes &lt;code&gt;.htaccess&lt;/code&gt; reads &lt;code&gt;/&lt;/code&gt; not as website's root directory (&lt;code&gt;/var/www/html&lt;/code&gt; or &lt;code&gt;C:/xampp/htdocs&lt;/code&gt;), but as filesystem's root directory (&lt;code&gt;/&lt;/code&gt; or &lt;code&gt;C:/&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;So, using domain and absolute path in general assures us, that no matter where 404 will happen, Apache will always find valid error document.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;robots.txt&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Imagine this:&lt;/p&gt;

&lt;p&gt;You've created a website. What to do now?&lt;/p&gt;

&lt;p&gt;Why not publish it and get it &lt;strong&gt;indexed&lt;/strong&gt; (visible) at Google (and other search engines)&lt;/p&gt;

&lt;p&gt;But we don't really want to show our style sheets or account configuration panel in Google, do we?&lt;br&gt;
That's the main purpose of &lt;strong&gt;robots.txt&lt;/strong&gt; files&lt;/p&gt;

&lt;p&gt;Google defines it as&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[robots.txt] tells search engine crawlers which URLs the crawler can access on your site&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I highly recommend you to get familiar with &lt;a href="https://developers.google.com/search/docs/crawling-indexing/robots/intro#:~:text=A%20robots.txt%20file%20tells,or%20password%2Dprotect%20the%20page."&gt;robots.txt docs&lt;/a&gt; from Google, before publishing the website&lt;/p&gt;

&lt;p&gt;Now, let's say we don't want to index &lt;code&gt;topsecretpage.php&lt;/code&gt;.&lt;br&gt;
We need to specify:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To &lt;strong&gt;What&lt;/strong&gt; crawlers this rule should be applied
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User-Agent: *
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;*&lt;/code&gt; stands for &lt;strong&gt;all&lt;/strong&gt; (just like in SQL)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Where&lt;/strong&gt; crawlers can't or can enter
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Disallow: /topsecretpage.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Allow: /openpage.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;robots.txt&lt;/code&gt; we also declare &lt;strong&gt;sitemap&lt;/strong&gt; - &lt;a href="https://simple.wikipedia.org/wiki/XML"&gt;XML&lt;/a&gt; file containing every sub-page on your website&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sitemap: https://www.example.com/sitemap.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Security issues
&lt;/h2&gt;

&lt;p&gt;Don't use &lt;code&gt;robots.txt&lt;/code&gt; to hide pages from everyone - this file is easily accessible.&lt;/p&gt;

&lt;p&gt;For example - this is YouTube's &lt;code&gt;robots.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxqjibpv0oznli83ehg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxqjibpv0oznli83ehg0.png" alt="robots.txt from YouTube" width="481" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I didn't hack YT or anything - just went to &lt;a href="https://www.youtube.com/robots.txt"&gt;https://www.youtube.com/robots.txt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So yeah, &lt;code&gt;robots.txt&lt;/code&gt; is cool to hide style sheets or some files that require logging in - but don't put credential or employee-only files in there&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you seen, this was more of site administration article - but hey! &lt;br&gt;
Every knowledge is valuable - especially if it resolves somewhere around our main interest&lt;/p&gt;

&lt;p&gt;Thanks for reading - check out &lt;a href="https://wizarddos.github.io/blog/"&gt;my blog&lt;/a&gt; and &lt;a href="https://wizarddos.github.io/blog/series/php_0_to_hero"&gt;rest of this series&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They all will get a re-write soon - so more valuable content and better summarized knowledge for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Little personal afterword
&lt;/h3&gt;

&lt;p&gt;As that's the last part of this "chapter" - I'd like to say something&lt;br&gt;
Thank you so much for every view, comment and like - Even though it wasn't much, still I'm glad that I could help someone.&lt;/p&gt;

&lt;p&gt;Also, big thanks to everyone who took their time and reviewed this course - from &lt;a href="https://forum.pasja-informatyki.pl/590068/moj-kurs-php-prosba-o-feedback"&gt;Forum Pasja Informatyki&lt;/a&gt; and &lt;a href="https://www.reddit.com/r/PHPhelp/comments/1aqxonn/my_php_course_request_for_your_feedback/"&gt;Reddit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll definitely post something like this again - when my work will reach satisfying level&lt;/p&gt;

&lt;p&gt;I've learned a lot, I hope you had as well - and what more can I say.&lt;/p&gt;

&lt;p&gt;Thanks for everything and see you while creating projects (and in some other articles)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Refactoring spaghetti code in PHP</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Fri, 23 Feb 2024 21:15:10 +0000</pubDate>
      <link>https://dev.to/wizarddos/cleaning-spaghetti-in-php-2144</link>
      <guid>https://dev.to/wizarddos/cleaning-spaghetti-in-php-2144</guid>
      <description>&lt;p&gt;Have you ever looked at the code and thought, "What is this mess"?&lt;/p&gt;

&lt;p&gt;After a while, you can even say such thing about your own "masterpieces"&lt;/p&gt;

&lt;p&gt;But remember, code review is part of the journey and perfect way to become 10x developer&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq26zj89p6q18a6enss4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq26zj89p6q18a6enss4m.png" alt="Only true code quality measure" width="550" height="433"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(Only true code quality measure)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Today, we'll learn some secrets of refactoring - so your next project will look as clear as a whistle&lt;/p&gt;

&lt;p&gt;Read the whole article carefully - You can take some breaks but don't skip the paragraphs&lt;/p&gt;

&lt;p&gt;Yet, no more intro - let's go&lt;/p&gt;
&lt;h2&gt;
  
  
  What will we be working on?
&lt;/h2&gt;

&lt;p&gt;I think this is a perfect example of &lt;strong&gt;spaghetti code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="c1"&gt;// Main code&lt;/span&gt;
    &lt;span class="nv"&gt;$h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$d&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$h&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$h&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$d&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Price is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid price!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$j&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$j&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fair amount of mess I sense. &lt;/p&gt;

&lt;p&gt;Overall - it's calculating total and quantity of a cart at some shopping website.&lt;/p&gt;

&lt;p&gt;Now imagine, it's your first day at a new workplace, you are assigned fix something in this chaos. &lt;br&gt;
And there are 1000 lines like this.&lt;/p&gt;

&lt;p&gt;Your whole shift is over, but you didn't even got half of the meaning - and bug fix is a whole different story&lt;/p&gt;

&lt;p&gt;So, how should we approach this?&lt;/p&gt;
&lt;h2&gt;
  
  
  Variable naming
&lt;/h2&gt;

&lt;p&gt;Don't you dare to name variables &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;c&lt;/code&gt; or so on&lt;br&gt;
(...Unless it's &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;for&lt;/code&gt; loop)&lt;/p&gt;

&lt;p&gt;That's the first thing we have to fix - as far as I understood&lt;br&gt;
&lt;code&gt;$h&lt;/code&gt; represents quantity of products&lt;br&gt;
&lt;code&gt;$i&lt;/code&gt; -&amp;gt; price&lt;br&gt;
&lt;code&gt;$h&lt;/code&gt; -&amp;gt; total&lt;br&gt;
&lt;code&gt;$j&lt;/code&gt; -&amp;gt; items&lt;br&gt;
&lt;code&gt;$k&lt;/code&gt; -&amp;gt; item (in for-each loop)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="c1"&gt;// Main code&lt;/span&gt;
    &lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Price is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid price!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks a little better, doesn't it&lt;/p&gt;

&lt;p&gt;But the end is far away - we've got more work to do&lt;/p&gt;

&lt;h2&gt;
  
  
  Modularization
&lt;/h2&gt;

&lt;p&gt;I don't think this listing is the only place in whole project, where we calculate total and display items from array &lt;/p&gt;

&lt;p&gt;Put these into functions - so we can reuse them later&lt;/p&gt;

&lt;p&gt;I'm starting with calculating total&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I can just write this over and over again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have also added types to be sure, that I won't pass &lt;code&gt;I like pizza&lt;/code&gt; as price of cart&lt;/p&gt;

&lt;p&gt;The same thing with listing items&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listItems&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, when you think about it. &lt;br&gt;
We'd probably have more arrays to list. Customers, orders, complains or employees, to name a few.&lt;/p&gt;

&lt;p&gt;So, why not write one function to list every array that we want to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much better - now we can use it all around the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, is it the end? Not really&lt;/p&gt;

&lt;h2&gt;
  
  
  DRY and KISS
&lt;/h2&gt;

&lt;p&gt;Are we doing a laundry? Will we put our computer into washing machine and then to leave in the sun?&lt;/p&gt;

&lt;p&gt;No, no, no - don't do that to your electronics&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DRY&lt;/strong&gt; is an acronym - stands for &lt;strong&gt;Don't repeat yourself&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In our case - we have those two snippets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Price is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid price!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second one is just expanded with quantity check - but does it have to be combined?&lt;/p&gt;

&lt;p&gt;That's what &lt;strong&gt;KISS&lt;/strong&gt; rule is about&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;KISS&lt;/strong&gt; means &lt;strong&gt;Keep it simple stupid&lt;/strong&gt; (I don't think that "stupid" here was meant to insult anyone)&lt;/p&gt;

&lt;p&gt;We can split this in two separate parts - no need to make a big pile out of it&lt;/p&gt;

&lt;p&gt;So, let's focus on second snippet - we'll divide it into 2 elements&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check total&lt;/li&gt;
&lt;li&gt;Check quantity&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We don't really need to write total calculation, as we have already done this - in &lt;code&gt;calculateTotal&lt;/code&gt; function&lt;/p&gt;

&lt;p&gt;Focus on quantity checking&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As in our spaghetti - quantity was only present when we calculated total, so we can call previously coded function&lt;/p&gt;

&lt;p&gt;Let's look at our code now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Significantly better than before, isn't it? But there are 2 more things to do&lt;/p&gt;

&lt;h2&gt;
  
  
  If-else maze
&lt;/h2&gt;

&lt;p&gt;Code looks fine, but we can make it even cleaner.&lt;/p&gt;

&lt;p&gt;Have a look at &lt;code&gt;checkQuantity()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can re-write it by changing if-else's conditions - I call it &lt;strong&gt;negative statements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, instead of checking if &lt;code&gt;$quantity&lt;/code&gt; is greater than 0, we check if it's less than 0 and finish this function when condition is met&lt;/p&gt;

&lt;p&gt;Like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much finer - now focus on &lt;code&gt;listArrayItems()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also see here nested statements - quick fix?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much better now - what does our code look like?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing! There's no comparison between refactored code and dirty one. &lt;br&gt;
That's the codebase you'd wish to work with&lt;/p&gt;

&lt;p&gt;We can theoretically end here, but I have one extra stage for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  Additional step: Modularization pt.2
&lt;/h2&gt;

&lt;p&gt;What if we want to use &lt;code&gt;listArrayItems()&lt;/code&gt; in some random file - but we don't need whole cart handling&lt;/p&gt;

&lt;p&gt;We can always move those functions to another file - and then include in everywhere needed&lt;/p&gt;

&lt;p&gt;I've created one called &lt;code&gt;cartModules.php&lt;/code&gt; with all functions inside&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; 

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is greater than 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Total is less than or equal to 100!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid quantity!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Quantity is greater than or equal to 10!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;listArrayItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid items!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No items to print!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;br&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, included it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"cartFunctions.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;checkQuantity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I can finally say - That's it&lt;/p&gt;

&lt;h2&gt;
  
  
  TL:DR - best practices in bullet points
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Give meaningful names to your variables&lt;/li&gt;
&lt;li&gt;Split code into functions, classes or namespaces&lt;/li&gt;
&lt;li&gt;DRY&lt;/li&gt;
&lt;li&gt;KISS&lt;/li&gt;
&lt;li&gt;Avoid nesting if-else statements - one layer of depth is enough. Two can slide, but three or more requires immediate code review&lt;/li&gt;
&lt;li&gt;Split your functions into reusable modules - and give them meaningful names too&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Just got the idea, so I wrote this article - hope you enjoyed it and learned something&lt;/p&gt;

&lt;p&gt;These are the basics - but with practice comes experience, so congrats. You have made the first step to make your scripts better&lt;/p&gt;

&lt;p&gt;But maybe, you are a pro in code review? Do you think there is something missing? Share your intriguing stories and personal practices in comments. &lt;/p&gt;

&lt;p&gt;By the way &lt;a href="https://wizarddos.github.io/blog/"&gt;check out my blog&lt;/a&gt;. I'm going to continue my PHP course there and create some awesome projects + there will be articles not published on dev.to&lt;/p&gt;

&lt;p&gt;Also, check out &lt;a href="https://wizarddos.github.io/blog/programming/2024/02/23/cleaning-spaghetii-code-in-php.html"&gt;this same article there&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code will be available on &lt;a href="https://github.com/wizarddos/Dev.to-scripts/tree/master/CodeRefactorPHP"&gt;Github&lt;/a&gt; both clean, and messy&lt;/p&gt;

&lt;p&gt;Again, I hope you've enjoyed it. Share your feedback, stories or ideas in comments and see you next time&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
      <category>refactorit</category>
      <category>php</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.15 - OOP coding</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 22 Feb 2024 21:22:10 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt15-oop-coding-5e0a</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt15-oop-coding-5e0a</guid>
      <description>&lt;p&gt;Hi there! Welcome back to another exciting part of our PHP journey. Today, we're delving into the captivating world of object-oriented programming (OOP). If you've just stumbled upon this article and OOP is a bit of a mystery to you, fear not! Check out the &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/14"&gt;previous part&lt;/a&gt; where I provided a comprehensive explanation of how OOP works.&lt;/p&gt;

&lt;p&gt;Now, let's focus on today's grand adventure, where we'll cover:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declaring classes and naming conventions&lt;/li&gt;
&lt;li&gt;Properties and methods with encapsulation&lt;/li&gt;
&lt;li&gt;How to declare constructors&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As an illustrative example, we'll create a class for handling database connections in PHP. So, buckle up, and let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes
&lt;/h2&gt;

&lt;p&gt;To create a class, we use the &lt;code&gt;class&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClassName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ever wondered why a class name starts with a capital letter? It's the &lt;code&gt;StudlyCaps&lt;/code&gt; convention, and it's the PSR naming convention for classes. So, remember, class names always start with a capital letter.&lt;/p&gt;

&lt;p&gt;Now, let's dive into the details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Properties and Methods
&lt;/h2&gt;

&lt;p&gt;I've covered these concepts theoretically in the &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/14"&gt;previous part&lt;/a&gt;, so if you missed it, take a moment to catch up.&lt;/p&gt;

&lt;p&gt;For the sake of readability, when creating a class, it's common practice to first declare properties and then methods. The first method declared should be the constructor, and the last should be the destructor.&lt;/p&gt;

&lt;p&gt;Now, let's start creating our DB connection class. Create a file called &lt;code&gt;db-class.php&lt;/code&gt; and declare the class &lt;code&gt;DatabaseHandler&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DatabaseHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Properties
&lt;/h3&gt;

&lt;p&gt;Now, let's declare the properties. We need DSN, database user, and database password:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$db_conn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we don't need to access these data from outside of this class, we've made them private. &lt;code&gt;$db_conn&lt;/code&gt; is protected, as it might be useful later.&lt;/p&gt;

&lt;p&gt;Our class now looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DatabaseHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$db_conn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Methods
&lt;/h3&gt;

&lt;p&gt;Why create a database handler if it can't handle database connections? Let's fix that. We'll start with a constructor. To declare it, we create a function called &lt;code&gt;__construct&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's add some content to our constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_dsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's what the constructor is for – initializing the class. And what's this mysterious &lt;code&gt;$this&lt;/code&gt;? It's a reference to the current object.&lt;/p&gt;

&lt;p&gt;Now, with the help of &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/12"&gt;this&lt;/a&gt;, &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/14"&gt;this&lt;/a&gt;, &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/13"&gt;this&lt;/a&gt;, and &lt;a href="https://wizarddos.github.io/blog/programming/php_0_to_hero/9"&gt;this&lt;/a&gt; article, I challenge you to write a method that executes SQL queries and returns their results as associative arrays. In the next article, I'll share my solution with comments on how it works.&lt;/p&gt;

&lt;p&gt;That's it! Our class is ready:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DatabaseHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$db_conn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_dsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;db_conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Place for your method&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inheritance
&lt;/h3&gt;

&lt;p&gt;Now, let's shift our focus to inheritance. In the example below, we'll use the same analogy as last time, but let's focus on the code rather than theory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$phone&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;talk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;swim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tom&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Tom class extends Person class&lt;/span&gt;
    &lt;span class="c1"&gt;// Tom inherits all public and protected members from Person&lt;/span&gt;

    &lt;span class="c1"&gt;// So class Tom will have these properties and methods:&lt;/span&gt;
    &lt;span class="c1"&gt;// public $name;&lt;/span&gt;
    &lt;span class="c1"&gt;// protected $age;&lt;/span&gt;
    &lt;span class="c1"&gt;// public function talk(){}&lt;/span&gt;
    &lt;span class="c1"&gt;// protected function walk(){}&lt;/span&gt;

    &lt;span class="c1"&gt;// But it will not inherit the private members&lt;/span&gt;
    &lt;span class="c1"&gt;// This is what object inheritance means&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tom&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a child class, we use the &lt;code&gt;extends&lt;/code&gt; keyword. The recipe for a child class is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChildName&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ParentName&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! The world of PHP OOP is full of exciting possibilities. Dive in and experiment&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To be fair, I thought I won't finish this article on time - but there it is!&lt;/p&gt;

&lt;p&gt;Check out my &lt;a href="https://wizarddos.github.io/blog/"&gt;blog&lt;/a&gt; and other articles there&lt;br&gt;
See you next time&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.14 - OOP Introduction</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 15 Feb 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt14-oop-introduction-36a9</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt14-oop-introduction-36a9</guid>
      <description>&lt;p&gt;Welcome again - we'll be starting a totally new topic now&lt;/p&gt;

&lt;p&gt;This article is purely theoretical - I want to familiarize you with OOP and it's basic principles&lt;/p&gt;

&lt;p&gt;So, what will we learn today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is OOP&lt;/li&gt;
&lt;li&gt;Basic concepts - like class, object, method, attribute etc.&lt;/li&gt;
&lt;li&gt;Encapsulation &lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sound complicated? It will be fine, don't worry&lt;/p&gt;

&lt;p&gt;So - let's go&lt;/p&gt;

&lt;h2&gt;
  
  
  What is OOP?
&lt;/h2&gt;

&lt;p&gt;OOP stands for &lt;strong&gt;Object Oriented Programming&lt;/strong&gt; - that's a really common principle in modern coding &lt;br&gt;
It's base is a concept of an &lt;strong&gt;object&lt;/strong&gt; - data structure containing both variables and functions?&lt;/p&gt;

&lt;p&gt;Do you remember connecting to the database? That's where we created objects - and then we used it's functions&lt;br&gt;
These functions are called &lt;strong&gt;methods&lt;/strong&gt; - remember that term, as I'll use it often&lt;br&gt;
Variables stored inside objects are called &lt;strong&gt;attributes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But how can we create object? Using &lt;strong&gt;classes&lt;/strong&gt; &lt;br&gt;
Class is a recipe for an object. It contains everything. attributes, constants, methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;someClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// all of the content here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 2 really special methods &lt;strong&gt;constructor&lt;/strong&gt; and &lt;strong&gt;destructor&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Constructor is a method, executed while creating an object. So calling like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Invokes a constructor - we can do whatever we want to with this method. I use it to assign values to attributes and log information about it.&lt;/p&gt;

&lt;p&gt;But destructor is the opposite of constructor (like name says). - It is executed, when object is destroyed. So mostly in the end. &lt;br&gt;
In lower-level languages such as C++ destructors where used to deallocate memory and just cleaning in general. Luckily working with addresses is not our job in PHP - so we can use it for logging purpose or just don't use it at all&lt;/p&gt;
&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&gt;

&lt;p&gt;Here, I'll use another analogy. Let's treat class as a mafia family.&lt;/p&gt;

&lt;p&gt;They have some assets, like cars, houses, stocks etc. But they don't want some of them, to be known for others.&lt;/p&gt;

&lt;p&gt;That's why they hide them. Set up fake bank accounts, transfer money, hide cars in garages all around the world and so on.&lt;/p&gt;

&lt;p&gt;And that's what encapsulation is all about - we make assets (attributes or methods in our case) visible only for specified group. So friend classes, everyone or no one.&lt;/p&gt;

&lt;p&gt;We have 3 basic clauses&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;public&lt;/code&gt; - Make it available for everyone. Other classes, other instances external code etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private&lt;/code&gt; - Hide it, it's visible only inside your class. We don't want to share it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;protected&lt;/code&gt; - only inheriting classes can access this (So in our analogy - children of mafia boss)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We add it before declaration of method/attribute inside class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$var&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;privateFunction&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="cm"&gt;/* more code here */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, I've told something about inheritance - what is it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;I wont' be explaining too much here, as I found (in my opinion) the best explanation for this concept in &lt;a href="https://www.php.net/manual/en/language.oop5.inheritance.php#121211"&gt;comments of PHP docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So thank you  Mohammad Istanbouly for this piece of explanation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$phone&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;talk&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;swim&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//Do stuff here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tom&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*Since Tom class extends Person class this means 
        that class Tom is a child class and class person is 
        the parent class and child class will inherit all public 
        and protected members(properties and methods) from
        the parent class*/&lt;/span&gt;

     &lt;span class="cm"&gt;/*So class Tom will have these properties and methods*/&lt;/span&gt;

     &lt;span class="c1"&gt;//public $name;&lt;/span&gt;
     &lt;span class="c1"&gt;//protected $age;&lt;/span&gt;
     &lt;span class="c1"&gt;//public function talk(){}&lt;/span&gt;
     &lt;span class="c1"&gt;//protected function walk(){}&lt;/span&gt;

     &lt;span class="c1"&gt;//but it will not inherit the private members &lt;/span&gt;
     &lt;span class="c1"&gt;//this is all what Object inheritance means&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this article explained a lot about OOP - I have shown you some code and we'll focus on writing next week.&lt;/p&gt;

&lt;p&gt;Share your feedback in the comments, check out &lt;a href="https://wizarddos.github.io/blog/"&gt;my blog&lt;/a&gt; and &lt;a href="https://wizarddos.github.io/blog/programming/2024/02/08/Why_is_hashing_passwords_important.html"&gt;this article about hashing&lt;/a&gt; and see you soon&lt;/p&gt;

</description>
      <category>php</category>
      <category>oop</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Why is hashing passwords important?</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 08 Feb 2024 22:25:41 +0000</pubDate>
      <link>https://dev.to/wizarddos/why-is-hashing-passwords-important-4go4</link>
      <guid>https://dev.to/wizarddos/why-is-hashing-passwords-important-4go4</guid>
      <description>&lt;p&gt;Passwords accompany us on regular basis.&lt;/p&gt;

&lt;p&gt;You know, every day we have to use them. If not them, some of substitutes ex. physical keys like Yubikey. &lt;/p&gt;

&lt;p&gt;Even though code security is frequently mentioned - we still hear about recent breaches/hacks and new vulnerabilities are disclosed on weekly (if not daily) basis.&lt;/p&gt;

&lt;p&gt;Most of web breaches work pretty simple - some endpoint was not sanitizing passed data properly, it lead to some code execution and then attacker somehow read whole database and it's now for sale on the dark web for $1000.&lt;/p&gt;

&lt;p&gt;But, what failed? How did the attacker extract data? Can we protect ourselves? &lt;/p&gt;

&lt;p&gt;Today, we will talk about securing passwords itself - and there is one really useful technique&lt;/p&gt;

&lt;p&gt;I'll tell you how it works, &lt;/p&gt;

&lt;h2&gt;
  
  
  What is hashing?
&lt;/h2&gt;

&lt;p&gt;Hashing is a form of converting data. Then, we end up with fixed-size output corresponding to passed data.&lt;/p&gt;

&lt;p&gt;It's a mathematical term, but crucial in IT security.&lt;/p&gt;

&lt;p&gt;The vital part of hashing is it's &lt;strong&gt;irreversibility&lt;/strong&gt; - Data once hashed, can't be deciphered unless we use more black-hat techniques (cracking for example)&lt;/p&gt;

&lt;p&gt;If we want to hash &lt;code&gt;hello&lt;/code&gt; using SHA-256 algorithm it, we end up with something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And even if we added another thousand random letters - result would also be ideally 64 characters long.&lt;/p&gt;

&lt;p&gt;last important thing - &lt;strong&gt;salt&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Salting passwords
&lt;/h3&gt;

&lt;p&gt;Just like you salt your meals to make them tastier - you salt your passwords but to be more secure&lt;/p&gt;

&lt;p&gt;What is a salt? It's a random chunk of bytes added to the string, before hashing - so cracking it is harder&lt;/p&gt;

&lt;p&gt;I assume that you know that passwords strength is based off on their length and randomness. It's definitely easier to guess&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Than&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kj8%'!kn)-~Yv*b4hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that string - &lt;code&gt;Kj8%'!kn)-~Yv*b4&lt;/code&gt; - is a salt&lt;/p&gt;

&lt;p&gt;We should always add those type of strings to passwords - yet here are some general rules to follow&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Salt has to be random - Always

&lt;ul&gt;
&lt;li&gt;Don't reuse salts for multiple users&lt;/li&gt;
&lt;li&gt;Don't use usernames as salts&lt;/li&gt;
&lt;li&gt;Don't set one salt for every password&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To generate it use &lt;a href="https://cryptobook.nakov.com/secure-random-generators"&gt;cryptographically secure&lt;/a&gt; generator - so the salt can't be guessed easily.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, why do we need it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need hashes?
&lt;/h2&gt;

&lt;p&gt;Time to answer that question - &lt;strong&gt;Why should you use hashing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'll sum this up in bullet points&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It adds another layer of security - Now, even if attacker gains access to the database, they need to spend time (and resources) to crack those hashes. Which might make a hack unprofitable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prevents from privacy violation - Let's be real, most of people reuse passwords. &lt;br&gt;
We are humans too and might have some personal affairs, better or worse. If an employee can have access to someone's password, they can (and maybe will) try it somewhere (like facebook or instagram). And if the password is reused - you know what might happen next.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We as creators don't want to be responsible any for these - so hash your passwords. That's how we can prevent those situations from happening&lt;/p&gt;

&lt;h2&gt;
  
  
  Hashing algorithms
&lt;/h2&gt;

&lt;p&gt;Bold of you to assume, that there is only one algorithm - There are THOUSANDS of them. Some are less, some more secure.&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#introduction"&gt;OWASP&lt;/a&gt; the best password hash is &lt;strong&gt;Argon2id&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why? As it provides a great defense against both GPU-based (so, to simplify guessing) and side-channel (exploiting flaws in algorithm implementation) attacks&lt;/p&gt;

&lt;p&gt;But it can't be applied everywhere - so we need to look for something else&lt;/p&gt;

&lt;p&gt;Then OWASP suggests &lt;strong&gt;scrypt&lt;/strong&gt; as backup option - Comparing to Argon2id&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These configuration settings provide an equal level of defense [in comparison with Argon2id]. The only difference is a trade off between CPU and RAM usage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are also 2 other ones to remember&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;bcrypt&lt;/strong&gt; - is meant to be utilized, when we interact with legacy systems. It has it's flaws, such as limiting input to 72 bytes so if possible, use either &lt;code&gt;Argon2id&lt;/code&gt; or &lt;code&gt;scrypt&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Legacy hashing algorithms
&lt;/h3&gt;

&lt;p&gt;Of course, except for the ones mentioned - there are older algorithms such as &lt;strong&gt;md5&lt;/strong&gt; or &lt;strong&gt;sha-1&lt;/strong&gt;. &lt;br&gt;
But they shouldn't be used - NEVER&lt;/p&gt;

&lt;p&gt;With modern technology we can pretty easily brute-force them. &lt;/p&gt;

&lt;p&gt;Every algorithm will one day become outdated - stay alerted and read about new hashes, so your data will always be secure&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As developers, we should treat security seriously. Don't overlook it, as simple breach can tear apart company's reputation and make you pay enormous fines&lt;/p&gt;

&lt;p&gt;I hope this article was useful, share your feedback down below&lt;br&gt;
Check out other articles such as &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://wizarddos.github.io/blog/cs/2023/11/01/Quick_Guide_For_linux_priviledges.html"&gt;Quick guide for linux privileges&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wizarddos.github.io/blog/programming/2023/12/27/Quick_guide_for_PHP_exceptions.html"&gt;Quick guide for PHP exceptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wizarddos.github.io/blog/programming/2023/12/04/PHP_echo_tag.html"&gt;PHP echo tag - A simple solution to common problem &lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And just follow my blog in general. If you want me to write about something, share it. I'll gladly publish a post, especially for you&lt;/p&gt;

&lt;p&gt;Stay safe and well informed. &lt;br&gt;
See you next time&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>backend</category>
      <category>security</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.13 - functions, include and require - PHP modules</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 08 Feb 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt13-functions-include-and-require-php-modules-1pe1</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt13-functions-include-and-require-php-modules-1pe1</guid>
      <description>&lt;p&gt;Welcome - It's 13th part of this course&lt;/p&gt;

&lt;p&gt;Today, we will get to something chill and easy - so we can rest a little after previous 4 parts of - I need to admit this - intensive work&lt;/p&gt;

&lt;p&gt;We'll focus on creating clean code and dividing it into modules&lt;/p&gt;

&lt;p&gt;So, what will we talk about today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What are functions and how to declare them&lt;/li&gt;
&lt;li&gt;How to get code from another file&lt;/li&gt;
&lt;li&gt;Advise on splitting code into modules&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;include&lt;/code&gt; vs &lt;code&gt;require&lt;/code&gt; - what's the difference&lt;/li&gt;
&lt;li&gt;Why do we sometimes add &lt;code&gt;_once&lt;/code&gt; to both &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this said - let's board and start this amazing journey&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Let's say, we are writing a game and sometimes our character dies - so we need to code the death of it&lt;/p&gt;

&lt;p&gt;Will we create a piece of code and then copy it around? Hell no, that's a violation of &lt;strong&gt;DRY principle&lt;/strong&gt; ("Don't repeat yourself")&lt;/p&gt;

&lt;p&gt;And if we wanted to change something? And there'd be 5000 of those death moments - have mercy for yourself&lt;/p&gt;

&lt;p&gt;That's why we have functions - right now we will only create one piece of code and put it in a function. &lt;/p&gt;

&lt;p&gt;Then, when we need it - just call the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;protagnist_death&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How do we declare them?&lt;/p&gt;

&lt;h3&gt;
  
  
  Declarating functions
&lt;/h3&gt;

&lt;p&gt;We declare them in blocks - using &lt;code&gt;function&lt;/code&gt; keyword&lt;/p&gt;

&lt;p&gt;Let's say we we need to validate a password - is it at least 8 characters, doesn't it contain username etc.&lt;/p&gt;

&lt;p&gt;So - start with the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, we specify that this is a function&lt;br&gt;
Then declarate it's name&lt;br&gt;
And open brackets for arguments&lt;/p&gt;

&lt;p&gt;Basically it's good practice to pass every required variable as an argument - so we operate only on them or also on variables declarated inside the function&lt;/p&gt;

&lt;p&gt;Here we only have one but we can always add more&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$minLength&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and so on, you add arguments after the commas (&lt;code&gt;,&lt;/code&gt;) - as much as you want (and need)&lt;/p&gt;

&lt;p&gt;Finish our function - let's do the second variant&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$minLength&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$minLength&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Length must be a number"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;$minLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Password is too short"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Password can't contain username"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Password correct"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, I hadn't wrote curly brackets in &lt;code&gt;if&lt;/code&gt; statements - that's just a shorter form of it.&lt;/p&gt;

&lt;p&gt;If you write one line, you don't have to create a big block - just indicate that &lt;code&gt;return&lt;/code&gt; is meant to execute after condition is met and that's it&lt;/p&gt;

&lt;p&gt;And what's that &lt;code&gt;return&lt;/code&gt;? It's basically what will remain after the execution (If you understand that concept from maths - it's like &lt;code&gt;y&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;So, let's now write a program that will use this function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$username_g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"test1"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$password_g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tes12345"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password_g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username_g&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Password correct &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, we call a function and pass our arguments - it then returns us the result&lt;/p&gt;

&lt;p&gt;So, with that declarated variables - the interpreter will see this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$username_g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"test1"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$password_g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tes12345"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Password correct"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's exactly what we have written in last return case &lt;/p&gt;

&lt;p&gt;You can test this code with multiple data, but let's now jump to another thing&lt;/p&gt;

&lt;h2&gt;
  
  
  Including files in PHP code
&lt;/h2&gt;

&lt;p&gt;We have a lot of files, that connect to the database - but we always need to pass there credentials. And what if they change?&lt;/p&gt;

&lt;p&gt;We'll have to get through all of the files and change there data? This sound horrifying &lt;/p&gt;

&lt;p&gt;That's why, we create a file called &lt;code&gt;config.php&lt;/code&gt; where we store all database info&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$db_dsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"OUR DSN"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can add it, but how? With function called &lt;code&gt;require&lt;/code&gt; or another one called &lt;code&gt;include&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, we connect to the database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"config.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// [...] Some code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use &lt;code&gt;include&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s2"&gt;"config.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// [...] Some code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the interpreter, sees this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$db_dsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"OUR DSN"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$db_dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$db_pass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// [...] Some code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful, isn't it?&lt;/p&gt;

&lt;p&gt;But, is there any real difference between &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt;? Yes, there is&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;include&lt;/code&gt; vs &lt;code&gt;require&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;There is one very big difference between them - error reporting&lt;/p&gt;

&lt;p&gt;Let's say, we want to include file called &lt;code&gt;header.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is one problem - that file does not exist. &lt;br&gt;
So, when we run &lt;code&gt;include&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s2"&gt;"header.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will just throw a warning (E_WARNING) and that's it - script continues like nothing happened&lt;/p&gt;

&lt;p&gt;But, if we use &lt;code&gt;require&lt;/code&gt; instead&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"header.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our script will crash and throw a fatal error (E_COMPILE_ERROR).&lt;/p&gt;

&lt;p&gt;So, when we have some crucial mechanisms - use &lt;code&gt;require&lt;/code&gt;&lt;br&gt;
If site will work fine without it, or could just display an error - use  &lt;code&gt;include&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, the last thing about those statements&lt;/p&gt;
&lt;h3&gt;
  
  
  What does appending &lt;code&gt;_once&lt;/code&gt; do?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;require&lt;/code&gt; and &lt;code&gt;include&lt;/code&gt; are not the only options. We can append &lt;code&gt;_once&lt;/code&gt; - and get &lt;code&gt;require_once&lt;/code&gt; and &lt;code&gt;include_once&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;They are used exactly the same and have the same differences - but how they differ from &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;include&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;If you happen to have some code, that might include the same file twice, using &lt;code&gt;*_once&lt;/code&gt; variant will include it only once&lt;/p&gt;

&lt;p&gt;So, if we included something twice - like here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="s2"&gt;"config.php"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// [...] Some code [...]&lt;/span&gt;
&lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="s2"&gt;"config.php"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interpreter would only see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$db_dsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"OUR DSN"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$db_pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// [...] Some code [...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without redeclarating variables in the code&lt;/p&gt;

&lt;h3&gt;
  
  
  Which one is the best?
&lt;/h3&gt;

&lt;p&gt;Right now, If you write clean code, it doesn't matter which one you use. &lt;/p&gt;

&lt;p&gt;Personally - I mostly utilize &lt;code&gt;require_once&lt;/code&gt;. But that's more of a habit.&lt;/p&gt;

&lt;p&gt;So, choice is yours!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Another part finished - hope you liked it&lt;/p&gt;

&lt;p&gt;By the way if you have some ideas for articles, share them in comments. I'll try to write about all of them.&lt;/p&gt;

&lt;p&gt;That's it - check out &lt;a href="https://wizarddos.github.io/blog/categories/programming"&gt;other articles&lt;/a&gt; and &lt;a href="https://wizarddos.github.io/blog/"&gt;my blog in general&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you next time&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.11 - Integrating SQL with PHP (SQL pt.3)</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 01 Feb 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt11-integrating-sql-with-php-sql-pt3-38ne</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt11-integrating-sql-with-php-sql-pt3-38ne</guid>
      <description>&lt;p&gt;Hi and hello&lt;/p&gt;

&lt;p&gt;Welcome in part 11 of this course - Today we will test our knowledge from 2 previous parts and add something new.&lt;br&gt;
What would it be? We'll be writing login script but in 2 ways. More about later&lt;/p&gt;

&lt;p&gt;So, what will we learn today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to use &lt;code&gt;mysqli&lt;/code&gt; both in procedural and object-oriented way&lt;/li&gt;
&lt;li&gt;How to use some other database managements systems&lt;/li&gt;
&lt;li&gt;What is PDO - why and how to use it&lt;/li&gt;
&lt;li&gt;What's SQL injection and how to defend our app against it&lt;/li&gt;
&lt;li&gt;What are prepared statements and how they are superior to other methods&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kind a lot of it - and we will write the same functionality 2 times&lt;/p&gt;

&lt;p&gt;So prepare yourself for a bit of reading&lt;br&gt;
I'd divide this into 2 articles - both will be here and on my blog and I'll try to publish them at the same time&lt;/p&gt;
&lt;h2&gt;
  
  
  Login mechanism
&lt;/h2&gt;

&lt;p&gt;Before we start writing - we should have some plan. How will our code even work?&lt;/p&gt;

&lt;p&gt;I have some ideas - here they are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First - save both &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; to variables and sanitize them&lt;/li&gt;
&lt;li&gt;Then execute a query that checks if passed &lt;code&gt;username&lt;/code&gt; is in our table 

&lt;ul&gt;
&lt;li&gt;If there is no user like that, redirect to the login page with message &lt;code&gt;Invalid username or password&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If there is a user - continue a script&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;password_verify&lt;/code&gt; function check if passwords match

&lt;ul&gt;
&lt;li&gt;If yes, set &lt;code&gt;isLoged&lt;/code&gt; session variable to &lt;code&gt;true&lt;/code&gt; and redirect to the secret page&lt;/li&gt;
&lt;li&gt;If not, redirect to the login page with message &lt;code&gt;Invalid username or password&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why do we display the same messages? Purely for security reasons&lt;br&gt;
That's how we hide from attacker contents of our database - even though username is valid potential hacker won't see it, as both messages are the same&lt;/p&gt;

&lt;p&gt;So, it's high time to start coding&lt;/p&gt;
&lt;h2&gt;
  
  
  Form template
&lt;/h2&gt;

&lt;p&gt;I won't be spending here too much time - this will be simple form with 2 inputs and a button&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; &lt;span class="nb"&gt;session_start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Log in&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Log in&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"login.php"&lt;/span&gt; &lt;span class="na"&gt;method = &lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name = &lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log in&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
        &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; 
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
                &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
                &lt;span class="k"&gt;unset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've also added php code for displaying eventual errors - it works like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if $_SESSION['error'] is set&lt;/li&gt;
&lt;li&gt;Display it&lt;/li&gt;
&lt;li&gt;Unset it - So it won't be here after refreshing the page&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'll skip styling - you can do it yourself and let's now focus on actual login - but first&lt;/p&gt;

&lt;h2&gt;
  
  
  Ways of connecting to database from PHP
&lt;/h2&gt;

&lt;p&gt;So, there is no one way? &lt;/p&gt;

&lt;p&gt;No, there is no one way. We have at least 3&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Via procedural interface of DBMS&lt;/li&gt;
&lt;li&gt;Via object-oriented interface of DBMS&lt;/li&gt;
&lt;li&gt;Using PDO&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Using DBMS's interface
&lt;/h2&gt;

&lt;p&gt;What do all of those geeky-sounding terms even it mean? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Procedural&lt;/strong&gt; means using &lt;strong&gt;functions&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Object-oriented&lt;/strong&gt; means using &lt;strong&gt;objects&lt;/strong&gt;&lt;br&gt;
Right now, I won't be diving into details of OOP (Object-oriented programming) - this will be later&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Right now, let's simplify object to this definition&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Object - data structure containing both functions and variables&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see the comparison - here with MySQL&lt;/p&gt;

&lt;p&gt;That's how the procedural version looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$mysqli&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;mysqli_connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"database"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mysqli_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$mysqli&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"SELECT `username` FROM `users`"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mysqli_fetch_assoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see - first we call a function called &lt;code&gt;mysqli_connect&lt;/code&gt; and save it's result to &lt;code&gt;$mysqli&lt;/code&gt; variable. It creates connection between our code and database&lt;br&gt;
Then with &lt;code&gt;mysqli_query&lt;/code&gt; we execute our query and save it's results to &lt;code&gt;$result&lt;/code&gt; &lt;br&gt;
After all we use &lt;code&gt;mysqli_fetch_assoc&lt;/code&gt; to fetch one row of data from &lt;code&gt;$result&lt;/code&gt; and assign it to &lt;code&gt;$row&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;See how in &lt;code&gt;mysqli_query&lt;/code&gt; we have to pass &lt;code&gt;$mysqli&lt;/code&gt; as parameter to the function&lt;/p&gt;

&lt;p&gt;Now let's see the same thing - but this time object-oriented&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$mysqli&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;mysqli&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"database"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$mysqli&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT `username` FROM `users`"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetch_assoc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's doing absolutely the same thing&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates &lt;code&gt;mysqli&lt;/code&gt; object inside &lt;code&gt;$mysqli&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Runs query using function from the object - then saves result to &lt;code&gt;$result&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Fetches data from &lt;code&gt;$result&lt;/code&gt; to the &lt;code&gt;$row&lt;/code&gt; array&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But look, how while executing query it doesn't take &lt;code&gt;$mysqli&lt;/code&gt; as parameter - why is that?&lt;br&gt;
Because while creating &lt;code&gt;$mysqli&lt;/code&gt; object it "saves" data inside it, so when function is called it can take that data from the object itself - no need to pass additional parameters&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-&amp;gt;&lt;/code&gt; calls a functions from inside the object - basic syntax looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$object&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both forms are valid - use whichever one you prefer&lt;br&gt;
I find object-oriented way better than procedural, so I'll write in this style&lt;/p&gt;

&lt;p&gt;With this said - let's write our login script&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing login script
&lt;/h2&gt;

&lt;p&gt;So, before we even start with writing Database connection - we have to do 2 more things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if we got the request&lt;/li&gt;
&lt;li&gt;Save values from &lt;code&gt;$_POST&lt;/code&gt; to variables&lt;/li&gt;
&lt;li&gt;Validate and sanitize them&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should be familiar with those, at this point - If not, check out parts &lt;a href="https://wizarddos.github.io/blog/programming/PHP_0_to_hero/7"&gt;7&lt;/a&gt; and &lt;a href="https://wizarddos.github.io/blog/programming/PHP_0_to_hero/8"&gt;8&lt;/a&gt; and then come back here&lt;/p&gt;

&lt;p&gt;Also, start a session - if you know nothing about them check out &lt;a href="https://wizarddos.github.io/blog/programming/PHP_0_to_hero/5"&gt;part 5&lt;/a&gt; of this course&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nb"&gt;session_start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"No username specified"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"No password specified"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nv"&gt;$username_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;htmlentities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;ENT_QUOTES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"UTF-8"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$password_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;htmlentities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;ENT_QUOTES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"UTF-8"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also &lt;code&gt;empty()&lt;/code&gt; function returns &lt;code&gt;true&lt;/code&gt; if passed variable is empty - and we don't want empty username or password do we?&lt;/p&gt;

&lt;p&gt;With this said - let's start with database connection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;mysqli&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"localhost"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"firstproject"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see, creating new &lt;code&gt;mysqli&lt;/code&gt; object takes 4 arguments&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Host - where is your database located (URL/Address)&lt;/li&gt;
&lt;li&gt;Username - database username. If you installed MySQL and PHPMyAdmin manually, these credentials will differ - so you need to put your valid ones&lt;/li&gt;
&lt;li&gt;Pasword - password for database user - in XAMPP &lt;code&gt;root&lt;/code&gt; has no password&lt;/li&gt;
&lt;li&gt;Database name - here it's &lt;code&gt;firstproject&lt;/code&gt; because that's how we named our table 2 parts ago&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Is everything working? If not, just show the error, and terminate execution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;connect_errno&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"Error in database connection"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Personally, I love this name of the last function. It ideally pictures what it does - terminate further code execution)&lt;/p&gt;

&lt;p&gt;First, let's maybe check the if username even exists - so let's write the query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM `users` WHERE `username` = ?"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why do we have that &lt;code&gt;?&lt;/code&gt; there? Because we will be using &lt;strong&gt;prepared statements&lt;/strong&gt;. And it indicates, where to place passed values&lt;/p&gt;

&lt;p&gt;That's the safest and the best way of executing SQL queries in PHP&lt;br&gt;
It secures our code from possible attacks such as SQLi. But what's that?&lt;/p&gt;
&lt;h3&gt;
  
  
  What is SQL Injection (SQLi)
&lt;/h3&gt;

&lt;p&gt;To make it as short as possible - SQL injection that allows user to change SQL queries and in consequence gain access to vulnerable data (Like personal identifiable information - PII or passwords)&lt;/p&gt;

&lt;p&gt;It occurs when PHP treats user-passed data as valid SQL. So when?&lt;/p&gt;

&lt;p&gt;Let's say an attacker tries to compromise our website, so instead of normal username sends &lt;code&gt;' OR 1=1--&lt;/code&gt; and password consists of  some random characters&lt;/p&gt;

&lt;p&gt;Then, if we added it to query like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM `users` WHERE `username` ='"&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We end up with this query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`username`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;--'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It always returns &lt;code&gt;true&lt;/code&gt;, so we will gain access to the first account in database - and that's often admin account&lt;/p&gt;

&lt;p&gt;With properly implemented authentication this should not happen, but this is very dangerous vulnerability, especially in places that return data to the users (like showing tasks on our website)&lt;/p&gt;

&lt;p&gt;That's it from cybersec point of view - if you want to learn more, check out &lt;a href="https://portswigger.net/web-security/sql-injection"&gt;this module from Portswigger&lt;/a&gt; and let's come back to writing&lt;/p&gt;

&lt;h3&gt;
  
  
  Prepared statements - executing query
&lt;/h3&gt;

&lt;p&gt;First - create a statement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, pass the username and execute the query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bind_param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username_s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;using &lt;code&gt;bind_param()&lt;/code&gt; function we substitute that question mark with value from &lt;code&gt;$username_s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First parameter is a string of types - here I passed &lt;code&gt;s&lt;/code&gt;, as we have only one parameter with type &lt;code&gt;string&lt;/code&gt;. But there are also numerical types&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;i&lt;/code&gt; - int&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;d&lt;/code&gt; - double&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example &lt;a href="https://www.php.net/manual/en/mysqli-stmt.bind-param.php"&gt;from the docs&lt;/a&gt; shows it perfectly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$mysqli&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bind_param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sssd'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$language&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$official&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$percent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'DEU'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$language&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bavarian'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$official&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"F"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$percent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;11.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to check if we got something and get results of that query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get_result&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;num_rows&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"invalid username or password "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetch_assoc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;num_rows&lt;/code&gt; is a variable inside &lt;code&gt;$result&lt;/code&gt; that stores integer representing how much rows were returned&lt;/p&gt;

&lt;p&gt;Now in &lt;code&gt;$row&lt;/code&gt; we have beautiful associative array with id, username and password &lt;/p&gt;

&lt;h3&gt;
  
  
  Validation
&lt;/h3&gt;

&lt;p&gt;Now we need only to validate if the password matches the hash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;password_verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"invalid username or password "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it does - close the connection, set &lt;code&gt;isLoged&lt;/code&gt; variable and redirect to the secret page&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'isLoged'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: secret.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this variable we'll ensure, that only authenticated users will have access to secret page&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;secret.php&lt;/code&gt; and inside it check if &lt;code&gt;isLoged&lt;/code&gt; is set&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; 
&lt;span class="nb"&gt;session_start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'isLoged'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I created a simple HTML webpage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    Welcome - you have successfully logged in
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it - you've created a login script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's the first half of this article - As I said before, I decided to split it into  2 parts, because &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One part would be too long&lt;/li&gt;
&lt;li&gt;This would be too much new knowledge for one article&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Second half will be posted soon - both here and on my blog&lt;br&gt;
I hope you enjoyed this article, and learned something new&lt;/p&gt;

&lt;p&gt;Code from this part is available on &lt;a href="https://github.com/wizarddos/Dev.to-scripts/tree/master/PHP%200%20to%20hero/Part%2011%20-%20SQL%20pt.%203"&gt;my github&lt;/a&gt; and &lt;/p&gt;

&lt;p&gt;By the way, If you want to have earlier access to my new articles, follow &lt;a href="https://wizarddos.github.io/blog/"&gt;my blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it - check out my other articles too and see you in next ones&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.12 - Using PDO (SQL pt.4 )</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 01 Feb 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt12-using-pdo-sql-pt4--26i8</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt12-using-pdo-sql-pt4--26i8</guid>
      <description>&lt;p&gt;Welcome again - today we continue our journey with using SQL databases in PHP&lt;/p&gt;

&lt;p&gt;Today I'll show you pre-installed extension to PHP called &lt;strong&gt;PDO&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's (in my opinion) better way to connect to the database &lt;br&gt;
So what will we learn today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is PDO&lt;/li&gt;
&lt;li&gt;Why should you use it&lt;/li&gt;
&lt;li&gt;A little bit about exceptions and their handling &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And as I said in the previous part - we'll create the login script again - but today with PDO.&lt;/p&gt;

&lt;p&gt;So, with this said - let's go!&lt;/p&gt;
&lt;h2&gt;
  
  
  What is PDO and why should you use it
&lt;/h2&gt;

&lt;p&gt;To make it short and clear. &lt;br&gt;
It's an interface for accessing databases in PHP. Unlike &lt;code&gt;mysqli&lt;/code&gt; or &lt;code&gt;pg&lt;/code&gt;, that are meant to be used with specific DBMS, PDO works with every more popular one. &lt;/p&gt;

&lt;p&gt;So, no matter if we use MySQL, PostgreSQL or Oracle - the code is exactly the same in every single one.&lt;br&gt;
That's the biggest benefit - we don't have to re-write the entire codebase just to migrate from ex. MySQL to PostgreSQL. We just change a few settings and we are good to go&lt;/p&gt;

&lt;p&gt;Another thing are the exceptions - with native interfaces we have to detect that something it wrong and then throw excepition&lt;br&gt;
But PDO does it on it's own - our role is to catch, and handle it&lt;/p&gt;

&lt;p&gt;But, what are those &lt;strong&gt;exceptions&lt;/strong&gt;?&lt;/p&gt;
&lt;h2&gt;
  
  
  Quick intro to exceptions
&lt;/h2&gt;

&lt;p&gt;This won't be long - for more info and deeper understanding I encourage you to check out &lt;a href="https://wizarddos.github.io/blog/programming/2023/12/27/Quick_guide_for_PHP_exceptions.html"&gt;this article&lt;/a&gt; purely about them&lt;/p&gt;

&lt;p&gt;Exceptions are built-in mechanism that allows us to perfectly deal with every possible error, that might occur&lt;/p&gt;

&lt;p&gt;For example it we write a calculator and somehow value passed as divisor is equal to 0, instead of dividing and crashing the whole website - code just throws an exception and another block handles it in the way we want to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Let's say somewhere before we declarated variable called $divider&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$dividedValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$val&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nv"&gt;$divider&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Rest of the code...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DivisionByZeroError&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Warning. Division by zero - execution terminated"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$divider&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DivisionByZeroError&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Rest of the code...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DivisionByZeroError&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Warning. Division by zero - execution terminated"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This snippets do exactly what we talked about &lt;/p&gt;

&lt;p&gt;Again - to learn more, check out linked article and now let's write our login script in PDO&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing login script
&lt;/h2&gt;

&lt;p&gt;The only thing that is different is DB connection - we can copy the rest&lt;/p&gt;

&lt;p&gt;Copy &lt;code&gt;index.php&lt;/code&gt;, &lt;code&gt;secret.php&lt;/code&gt; and this from &lt;code&gt;login.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nb"&gt;session_start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"No username specified"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"No password specified"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$username_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;htmlentities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;ENT_QUOTES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"UTF-8"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$password_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;htmlentities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;ENT_QUOTES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"UTF-8"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// rest of the code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PDOException&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"Server error - "&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've also added &lt;code&gt;try-catch&lt;/code&gt; block for eventual errors and exceptions&lt;br&gt;
Right now, we want message displayed. On typical (production) server it should never happen - for security reasons &lt;/p&gt;

&lt;p&gt;We'll write rest of the code in place of comment - so inside &lt;code&gt;try&lt;/code&gt; block&lt;/p&gt;

&lt;p&gt;With this said let's start coding&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating DB connection
&lt;/h3&gt;

&lt;p&gt;First - create DB connection. But today with PDO&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"mysql:host=localhost;dbname=firstproject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see, instead of 4 it takes only 3 arguments - but the first one looks strange - let's analyze it&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;DSN&lt;/strong&gt; (Data source name) - that's a fancy name for a data structure (here string) that contains info necessary for PDO to connect to database &lt;br&gt;
(To be more specific, it's needed for &lt;a href="https://en.wikipedia.org/wiki/Open_Database_Connectivity"&gt;ODBC&lt;/a&gt; driver - the actual API for DB connection, which is used by PDO)&lt;/p&gt;

&lt;p&gt;So let's see what contains this string -&lt;code&gt;mysql:host=localhost;dbname=firstproject&lt;/code&gt;&lt;br&gt;
I'll divide it into 3 parts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;mysql:&lt;/code&gt; - indicated used DBMS&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;host=localhost;&lt;/code&gt; - Where is database located&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dbname=firstproject&lt;/code&gt; - database name&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are more of those declarations - you can check them &lt;a href="https://www.php.net/manual/en/ref.pdo-mysql.connection.php"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the only necessary ones for us - I hadn't specify port. We don't need it, as XAMPP (and your manual installation) uses default one - &lt;code&gt;3306&lt;/code&gt; if you want to know &lt;/p&gt;

&lt;p&gt;Right now, we don't need to check &lt;code&gt;errno&lt;/code&gt; inside PDO, as if something goes wrong - PDO will automatically throw an exception&lt;br&gt;
Comfy, isn't it&lt;/p&gt;
&lt;h3&gt;
  
  
  Executing query
&lt;/h3&gt;

&lt;p&gt;First - write the query (or copy it - as it's the same one)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM `users` WHERE `username` = ?"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use prepared statements again - so let's write it&lt;/p&gt;

&lt;p&gt;Start with preparing the query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="nv"&gt;$stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let's execute it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nv"&gt;$username_s&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But hold on a second - don't we have to bind the parameters?&lt;/p&gt;

&lt;p&gt;Actually, we do - but not like before. Let's have a look at previously called function&lt;/p&gt;

&lt;p&gt;As you see, we pass as it's parameter &lt;code&gt;$username_s&lt;/code&gt; - so we do bind that parameters, but in &lt;code&gt;execute()&lt;/code&gt;, not with &lt;code&gt;bindParam()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And why it's in square brackets? Because, &lt;code&gt;execute()&lt;/code&gt; only takes one parameter - and it's an array of arguments&lt;/p&gt;

&lt;p&gt;Wrapping &lt;code&gt;$username_s&lt;/code&gt; in square brackets we create an array with only one element - username&lt;/p&gt;

&lt;p&gt;But if our query required more parameters - we can add something to it, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nv"&gt;$arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$arg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$arg3&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(that's just example - not our code element)&lt;/p&gt;

&lt;p&gt;Of course, there is a function in PDO such as &lt;a href="https://www.php.net/manual/en/pdostatement.bindparam"&gt;bindParam&lt;/a&gt;, and we can do regular binding&lt;/p&gt;

&lt;p&gt;In this case - I'd look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bindParam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$username_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PARAM_STR&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First parameter means to which &lt;code&gt;?&lt;/code&gt; should this be bind &lt;br&gt;
Second is actual value&lt;br&gt;
And third - type of an argument, but using PDO constants. You can check their list &lt;a href="https://www.php.net/manual/en/pdo.constants.php"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Obtaining results
&lt;/h3&gt;

&lt;p&gt;So now, we have something left &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if we got something returned by query&lt;/li&gt;
&lt;li&gt;Check if passwords match&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First - we should get the results&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="nv"&gt;$results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;FETCH_ASSOC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function (&lt;code&gt;fetch&lt;/code&gt;) will get us 1 resuls from the query in a format specified as an argument&lt;/p&gt;

&lt;p&gt;And an argument is another PDO constant - here I've chosen associative array. But it can be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PDO::FETCH_NUM&lt;/code&gt; - an array indexed with numbers, not column names&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PDO::FETCH_OBJ&lt;/code&gt; - and object with variables inside, named correspondingly to column names&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PDO::FETCH_ASSOC&lt;/code&gt; - an associative array. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's check if we even got something&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$results&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"invalid username or password "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do we negate an array? It's not an expression. &lt;/p&gt;

&lt;p&gt;Yeah, in some way it it.&lt;br&gt;
Every value - no matter what type - has it's boolean value.&lt;br&gt;
For 0 and empty - it's &lt;code&gt;false&lt;/code&gt;&lt;br&gt;
For not 0 - it's &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So when we negate the array - interpreter "thinks" something like this&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So, we need to get boolean value of &lt;code&gt;$results&lt;/code&gt; - 0 for empty, 1 for not empty. Then, I negate it. So when &lt;code&gt;$results&lt;/code&gt;' value is 0 - if statement is true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this checked - let's look at the password&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;password_verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$password_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
        &lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;"invalid username or password "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: index.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think you saw it - I just copied the previous one changing &lt;code&gt;$row&lt;/code&gt; to &lt;code&gt;$result&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With this checked - we can redirect user to the secret page&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$_SESSION&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'isLoged'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Location: secret.php"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does it work? - It works&lt;/p&gt;

&lt;p&gt;And that's it - we finished this script&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thanks for reading - I hope you enjoyed it. &lt;br&gt;
To be fair, I thought this article will be published somewhere on Sunday/Monday but I managed to finish it on time - so you see both of those at the same time &lt;/p&gt;

&lt;p&gt;Whole code is available on &lt;a href="https://github.com/wizarddos/Dev.to-scripts/tree/master/PHP%200%20to%20hero/Part%2012%20-%20SQL%20pt.%204"&gt;my Github&lt;/a&gt; - today I also included the dump from database we created a while ago&lt;/p&gt;

&lt;p&gt;You can import it using &lt;code&gt;import&lt;/code&gt; functionality in PHPMyAdmin&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiudnskckycqb4h52git.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiudnskckycqb4h52git.png" alt="Import in nav" width="89" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it - share your feedback in the comments. Also remember to regularly check &lt;a href="https://wizarddos.github.io/blog/"&gt;my blog&lt;/a&gt; so you have access to my articles earlier. And not only to programming-based ones&lt;/p&gt;

&lt;p&gt;See you in next articles &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.10 - SQL basics</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 25 Jan 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt10-sql-basics-681</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt10-sql-basics-681</guid>
      <description>&lt;p&gt;When we have SQL database ready - let's start learning language itself&lt;/p&gt;

&lt;p&gt;SQL, as I said, stands for &lt;strong&gt;Structured Query Language&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It looks a bit like a sentence in English&lt;/p&gt;

&lt;p&gt;Now, we need to learn actual syntax and how to send those syntax  &lt;/p&gt;

&lt;p&gt;So, what do we do today?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is query and how to execute them in phpMyAdmin&lt;/li&gt;
&lt;li&gt;Different query types and their syntax&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's go&lt;/p&gt;

&lt;h2&gt;
  
  
  Running queries
&lt;/h2&gt;

&lt;p&gt;Before everything - what is query?&lt;/p&gt;

&lt;p&gt;Query is a "sentence" in SQL, that tells database to interact (Get, edit, delete) with records.&lt;/p&gt;

&lt;p&gt;You will see a lot of examples of them today&lt;/p&gt;

&lt;p&gt;But, how do we execute that query in phpMyAdmin?&lt;/p&gt;

&lt;p&gt;When we select table there is a little box above records:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy5it63tk8hnc8r259a9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy5it63tk8hnc8r259a9o.png" alt="Box" width="744" height="155"&gt;&lt;/a&gt;&lt;br&gt;
When we click &lt;code&gt;Edit&lt;/code&gt; or &lt;code&gt;Edit inline&lt;/code&gt; we can run our own queries on the table&lt;/p&gt;

&lt;p&gt;When we know, how to execute that query - let's learn some basic ones&lt;/p&gt;
&lt;h2&gt;
  
  
  Queries
&lt;/h2&gt;

&lt;p&gt;Don't get shocked - it's a best and very common practice to write SQL keywords in uppercase (with caps lock)&lt;/p&gt;

&lt;p&gt;So, when we write query&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;values defined by us, like table/column names  - lowercase&lt;/li&gt;
&lt;li&gt;SQL keywords (stuff built-in language) - uppercase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we choose the table, this one query is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is it?&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;SELECT&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; allows us to get every row that meets specific conditions&lt;/p&gt;

&lt;p&gt;For example: we want to select value from table &lt;code&gt;users&lt;/code&gt; from columns named &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;username&lt;/code&gt; where &lt;code&gt;email&lt;/code&gt; value equals &lt;code&gt;test@test.com&lt;/code&gt; &lt;br&gt;
Then, we'd write query like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;`username`&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`email`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"test@test.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember - conditions can be stacked using &lt;code&gt;AND&lt;/code&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;`username`&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`email`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"test@test.com"&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;`password`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"Some hashed Password"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can place as many &lt;code&gt;AND&lt;/code&gt; as you want&lt;/p&gt;

&lt;p&gt;So, formula for &lt;code&gt;SELECT&lt;/code&gt; query looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Columns&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to select values from every column for each record we use &lt;code&gt;*&lt;/code&gt; symbol&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`email`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"test@test.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's pretty much it - we'll get used to it when we'll be actually working with SQL&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;INSERT&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Let's say we want to add some record to the table - that's what &lt;code&gt;INSERT&lt;/code&gt; is for&lt;/p&gt;

&lt;p&gt;So, to add a new user to our &lt;code&gt;users&lt;/code&gt; table, just run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"user1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"$2y$10$7uKWbeYe7X/oZyQT/fxxfOp8ichcShxejqSXAOSYbMFiNCwkpe70."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last value is &lt;code&gt;bcrypt&lt;/code&gt; hash of &lt;code&gt;test123&lt;/code&gt; string&lt;/p&gt;

&lt;p&gt;Try it - click &lt;code&gt;Edit&lt;/code&gt; or &lt;code&gt;Edit inline&lt;/code&gt; (eventually &lt;code&gt;SQL&lt;/code&gt; in top nav) and paste that query&lt;/p&gt;

&lt;p&gt;After execution, we should see one record&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbj5qybi6yze3sz7pb9d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbj5qybi6yze3sz7pb9d.png" alt="Records from  raw `users` endraw " width="800" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why is first value &lt;code&gt;NULL&lt;/code&gt;? That's why we set up &lt;code&gt;Auto increment&lt;/code&gt; for this column - now when we pass null it replaces it with numerical value of previous record and adds 1&lt;/p&gt;

&lt;p&gt;So, if we run this query couple of times - then we get ids equal to &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;4&lt;/code&gt; and so on&lt;/p&gt;

&lt;p&gt;Here is the formula for &lt;code&gt;INSERT&lt;/code&gt; query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;Values&lt;/span&gt; &lt;span class="n"&gt;separated&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;comma&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where number of values passed has to match number of columns &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;DELETE&lt;/code&gt; query
&lt;/h3&gt;

&lt;p&gt;Our user doesn't really like his new task - he want to get rid of it&lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;DELETE&lt;/code&gt; comes in handy.&lt;/p&gt;

&lt;p&gt;With this task - let's see a quick example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`tasks`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`task_id`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;1&lt;/code&gt; here is just an example - in reality it would be value passed by PHP&lt;/p&gt;

&lt;p&gt;Whole "recipe" for this query looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;UPDATE&lt;/code&gt; query
&lt;/h3&gt;

&lt;p&gt;But if a user made a typo in it's title? Will we get whole contents of that record, then delete it and insert new?&lt;/p&gt;

&lt;p&gt;That's additional 3 queries. Multiplied by 1 million users it gives us 3 million queries - too much&lt;/p&gt;

&lt;p&gt;That's why we have &lt;code&gt;UPDATE&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="nv"&gt;`tasks`&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nv"&gt;`title`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"Something new"&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`task_id`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will set &lt;code&gt;title&lt;/code&gt; to &lt;code&gt;Something new&lt;/code&gt; in record with &lt;code&gt;task_id&lt;/code&gt; equal to 1 from &lt;code&gt;tasks&lt;/code&gt; table&lt;/p&gt;

&lt;p&gt;And basic syntax looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;table&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="k"&gt;values&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dangers in &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;While working with &lt;code&gt;DELETE&lt;/code&gt; and &lt;code&gt;UPDATE&lt;/code&gt; we need to be sure, that &lt;code&gt;WHERE&lt;/code&gt; conditions are valid - otherwise we might wake up with whole table looking identical or with no table at all&lt;/p&gt;

&lt;p&gt;Unless we are specifically sure, that we want to update every record in table or delete whole content of the table, don't forget &lt;code&gt;WHERE&lt;/code&gt; clause &lt;/p&gt;

&lt;h3&gt;
  
  
  Order of results
&lt;/h3&gt;

&lt;p&gt;So, we have a bunch of posts but if we want to show them - it will display the oldest ones first (as that's the order in database)&lt;/p&gt;

&lt;p&gt;Do we have to reverse that table in PHP itself? No - just use &lt;code&gt;ORDER BY&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example with tasks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`tasks`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`creator`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"johnny"&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nv"&gt;`task_id`&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DESC&lt;/code&gt; will order the results alphabetically reversed (So here, from the greatest to lowest id)&lt;/p&gt;

&lt;p&gt;And just &lt;code&gt;ORDER BY&lt;/code&gt; (or &lt;code&gt;ORDER BY [column] ASC&lt;/code&gt;) will return results in alphabetical order&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;LIKE&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;SQL &lt;code&gt;LIKE&lt;/code&gt; keyword allows us to search for specific pattern in column&lt;/p&gt;

&lt;p&gt;So, I'll give an example&lt;/p&gt;

&lt;p&gt;Let's say we create a shop and we have a database of cities&lt;/p&gt;

&lt;p&gt;We want to see every city that starts with the letter &lt;code&gt;L&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So - we write this query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;`Cities`&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`name`&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="nv"&gt;`L%`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From SQL to english&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;select all the columns from records from &lt;code&gt;Cities&lt;/code&gt; table where &lt;code&gt;name&lt;/code&gt; begins with &lt;code&gt;L&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;%&lt;/code&gt; is so-called &lt;strong&gt;wildcard&lt;/strong&gt; and it means any number of characters or even no at all&lt;/p&gt;

&lt;p&gt;There is also one more useful wildcard - &lt;code&gt;_&lt;/code&gt;&lt;br&gt;
It represents a single character&lt;/p&gt;

&lt;p&gt;So if in DB we'd have these 2 values&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Lon&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Lo&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With &lt;code&gt;%&lt;/code&gt;, both of those meet condition&lt;br&gt;
But is we use &lt;code&gt;_&lt;/code&gt; - only the second one will get returned&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's pretty much it - I hope you have learnt something, as I'll require that knowledge in the next part &lt;/p&gt;

&lt;p&gt;I thought, this part will have a delay (as I was working on something) but luckily it was short so it got posted in time&lt;/p&gt;

&lt;p&gt;What I was working on? &lt;a href="https://wizarddos.github.io/blog"&gt;On my own blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So yeah, most of the articles will get posted there - especially non-coding ones &lt;/p&gt;

&lt;p&gt;If you want to have access to this course earlier - check my blog, as I'll post those articles as soon as I finish writing them &lt;/p&gt;

&lt;p&gt;Also, check out my other articles and another parts of this course - and see you in the next ones&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>php</category>
    </item>
    <item>
      <title>PHP 0 to hero pt.9 - setting up SQL (SQL pt. 1)</title>
      <dc:creator>Mateusz Jasiński </dc:creator>
      <pubDate>Thu, 18 Jan 2024 15:00:00 +0000</pubDate>
      <link>https://dev.to/wizarddos/php-0-to-hero-pt9-setting-up-sql-sql-pt-1-4dad</link>
      <guid>https://dev.to/wizarddos/php-0-to-hero-pt9-setting-up-sql-sql-pt-1-4dad</guid>
      <description>&lt;p&gt;What's up - welcome in another part of this course&lt;/p&gt;

&lt;p&gt;Today, we start very important topic - using databases in PHP&lt;/p&gt;

&lt;p&gt;This part is an introduction - there will be 2 other parts about strictly coding&lt;/p&gt;

&lt;p&gt;So, what do we do today?&lt;/p&gt;

&lt;p&gt;We will&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Learn something about PHPMyAdmin and MySQL&lt;/li&gt;
&lt;li&gt;Create our first database&lt;/li&gt;
&lt;li&gt;Familiarize ourselves with terms like primary key, row, column, SQL, DBMS&lt;/li&gt;
&lt;li&gt;Data types in SQL-based databases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, let's get started&lt;/p&gt;

&lt;h2&gt;
  
  
  What will we use today
&lt;/h2&gt;

&lt;p&gt;This time, we will utilize another software - DBMS&lt;/p&gt;

&lt;p&gt;What is it? It stands for Database Management System&lt;br&gt;
There, my choice is MySQL - we have it installed with XAMPP &lt;br&gt;
(To be fair, it's MariaDB)&lt;/p&gt;

&lt;p&gt;But remember - that's not the only one&lt;/p&gt;

&lt;p&gt;We have plenty of those. There are differences in writing SQL code then - also PHP syntax might be different&lt;/p&gt;

&lt;p&gt;Some of the most popular are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.mysql.com/products/"&gt;MySQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mariadb.org/"&gt;MariaDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.microsoft.com/en-us/sql-server/sql-server-downloads"&gt;Microsoft SQL Server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oracle.com/database/"&gt;Oracle Database&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another thing is software - We have utility built in XAMPP (and installed in linux) called &lt;strong&gt;phpmyadmin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This allows us to work with database graphically, rather than via queries&lt;/p&gt;

&lt;p&gt;And, what is a query? That's a structure, that tells database to perform some action - like select some data or update them&lt;/p&gt;

&lt;p&gt;We write them in language called &lt;strong&gt;SQL&lt;/strong&gt; - structured query language&lt;/p&gt;

&lt;p&gt;Let's see what it really looks like&lt;/p&gt;

&lt;h2&gt;
  
  
  Launching PHPMyAdmin
&lt;/h2&gt;

&lt;p&gt;First thing - enable MySQL service in XAMPP (Or start &lt;code&gt;mysql-server&lt;/code&gt; and &lt;code&gt;mysql-client&lt;/code&gt; in linux)&lt;/p&gt;

&lt;p&gt;Then, when this and Apache is working - we can access phpmyadmin&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;http://localhost/phpmyadmin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At first - it looks like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnd9uht6trhvztpzab48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnd9uht6trhvztpzab48.png" alt="First glance" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Here I've got some databases from my previous projects - you should have it empty)&lt;/p&gt;

&lt;p&gt;We'll familiarize ourselves with some of these - right now click &lt;code&gt;new&lt;/code&gt; inside sidenav - you should be redirected to page like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uris67ktmzeat3hyih7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uris67ktmzeat3hyih7.png" alt="Creating database" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating database
&lt;/h2&gt;

&lt;p&gt;Let's create a database called &lt;code&gt;firstProject&lt;/code&gt; - choose whichever encoding suites your language from ones starting with &lt;code&gt;utf8&lt;/code&gt; and ending with &lt;code&gt;ci&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As I am polish, I've chosen &lt;code&gt;utf8_polish_ci&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After inputting everything - it looks like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajfwbr5etr0n8w5s8zww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajfwbr5etr0n8w5s8zww.png" alt="Ready to create database" width="699" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, next page&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmilpwrtq2es4420ouiv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmilpwrtq2es4420ouiv5.png" alt="Table creation view" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Database designing
&lt;/h2&gt;

&lt;p&gt;We will create simple to-do app - so we need 2 tables&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;users&lt;/code&gt; - It will have 3 rows (right now)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user_id&lt;/code&gt; - Numerical user id (It will be our &lt;strong&gt;primary key&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;username&lt;/code&gt; - self-explanatory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;password&lt;/code&gt; - user's password - I think also self explanatory&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;tasks&lt;/code&gt; - All tasks will be stored here. It will consist of 5 rows  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;task_id&lt;/code&gt; - Primary key for this table&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;task_title&lt;/code&gt; - self-explanatory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;description&lt;/code&gt; - description for user about specific task&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;creator&lt;/code&gt; - Task owner. It will be &lt;code&gt;user_id&lt;/code&gt; of whoever created this task&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;timestamp&lt;/code&gt; - when was task created&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What's a primary key? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Primary key&lt;/strong&gt; - It's a column in the table (most of the time numerical), that's unique to all records (rows) - like here id. &lt;br&gt;
It is used to distinguish rows in table&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating tables
&lt;/h2&gt;

&lt;p&gt;First, let's create &lt;code&gt;users&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add name and number of columns - here 3&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4insqc1bn2u3sze6u6ik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4insqc1bn2u3sze6u6ik.png" alt="What should it look like" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we end up here&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32bqjyfvti6wyn3ei7sf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32bqjyfvti6wyn3ei7sf.png" alt="Table creation page" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start with &lt;code&gt;Name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After inputting names that we agreed to, we should have something like this&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntc6uhjojrcp779ld2xs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntc6uhjojrcp779ld2xs.png" alt="View right now" width="553" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next part - &lt;strong&gt;type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This indicates what type of data will be stored there&lt;/p&gt;

&lt;p&gt;We have a bunch of them, like &lt;code&gt;DECIMAL&lt;/code&gt;, &lt;code&gt;BOOLEAN&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, what types will we have?&lt;br&gt;
&lt;code&gt;user_id&lt;/code&gt; - will be &lt;code&gt;INT&lt;/code&gt;&lt;br&gt;
&lt;code&gt;username&lt;/code&gt; - will be &lt;code&gt;TEXT&lt;/code&gt;&lt;br&gt;
&lt;code&gt;password&lt;/code&gt; - as well &lt;code&gt;TEXT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiz4f4cdux0ks6h110pai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiz4f4cdux0ks6h110pai.png" alt="Named columns with types" width="363" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we have this - one last thing&lt;/p&gt;

&lt;p&gt;We were talking about primary key - have we specified it? No&lt;/p&gt;

&lt;p&gt;So, let's do it&lt;/p&gt;

&lt;p&gt;There is a column called &lt;code&gt;index&lt;/code&gt; - in row with &lt;code&gt;user_id&lt;/code&gt; click on it, and choose &lt;code&gt;PRIMARY&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On pop-up - just click go and don't bother&lt;/p&gt;

&lt;p&gt;Right beside it - we have suspicious checkbox labeled as &lt;code&gt;A_I&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Is it to fill it with data generated by AI? Not really&lt;/p&gt;

&lt;p&gt;&lt;code&gt;A_I&lt;/code&gt; stands for &lt;code&gt;AUTO INCREMENT&lt;/code&gt; - we know the verb "to increment"&lt;/p&gt;

&lt;p&gt;So, this option means that when we pass an empty value here (&lt;code&gt;NULL&lt;/code&gt;) it will automatically replace it with incremented value of previous row&lt;/p&gt;

&lt;p&gt;So, if we added a new row here it would automatically set &lt;code&gt;user_id&lt;/code&gt; value to &lt;code&gt;1&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;If we added next row, it would fill it with &lt;code&gt;2&lt;/code&gt;. And so on&lt;/p&gt;

&lt;p&gt;If you hadn't realized it yet - we have to check it&lt;/p&gt;

&lt;p&gt;After all - it looks like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feenmgdgtsegyehu7oor1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feenmgdgtsegyehu7oor1.png" alt="Ready table structure" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;Save&lt;/code&gt; button below and boom - we should be redirected to page called &lt;code&gt;structure&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjf0bycgkzy83vgjt5anb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjf0bycgkzy83vgjt5anb.png" alt="Table structure" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks like it's fine - let's create the second one&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;New&lt;/code&gt; in database drop-down menu on navbar&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe968ziuyl41nnbyflhvm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe968ziuyl41nnbyflhvm.png" alt="Where to click" width="287" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Up here&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2imbdkah5pdr1g7eehd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2imbdkah5pdr1g7eehd.png" alt="place for table name" width="782" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add table name. We have only 4 colums, but we need 5&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;Go&lt;/code&gt; button beside table name - it will add a new column&lt;/p&gt;

&lt;p&gt;Now - fill it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8emnma3rohxsfyn5izt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8emnma3rohxsfyn5izt.png" alt="Filled table" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you see, I allowed myself to skip explaining everything - you should know, how it works and what some of those mean&lt;/p&gt;

&lt;p&gt;One thing I want to tell you about is &lt;code&gt;DATETIME&lt;/code&gt; type&lt;/p&gt;

&lt;p&gt;It allows us to store dates in standardized MySQL format - &lt;code&gt;YYYY-MM-DD hh:mm:ss&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, those letters stand for one digit of&lt;br&gt;
&lt;code&gt;Y&lt;/code&gt; - year&lt;br&gt;
&lt;code&gt;M&lt;/code&gt; - month &lt;br&gt;
&lt;code&gt;D&lt;/code&gt; - day&lt;br&gt;
&lt;code&gt;h&lt;/code&gt; - hour&lt;br&gt;
&lt;code&gt;m&lt;/code&gt; - minute&lt;br&gt;
&lt;code&gt;s&lt;/code&gt; - second&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;Save&lt;/code&gt; and job is done&lt;/p&gt;

&lt;p&gt;We have successfully created a database in PHPMyAdmin&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you see, today we have written no code - but don't worry.&lt;br&gt;
Next parts will have more of coding&lt;/p&gt;

&lt;p&gt;Check out other parts of this series - wait for next ones and see you in next articles &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
