Author Archives: Anu

Learning PHP The Right Way vol 2

Notes for myself from youtube tutorial Full PHP 8 Tutorial - Learn PHP The Right Way by Program With Gio. Video nr 4: What Are Constants & Variable Variables In PHP - Full PHP 8 Tutorial
  1. Constants - we can't overwrite values. Constants are defined using uppercase. There are two ways of defining constants:
    • define('name', 'value');
      <?php define ('STATUS_PASSED', 1);
      echo SATUS_PASSED
      ?>
      
      or
      <?php 
      $passed = 1;
      define('STATUS_'.$passed, 1);
      echo STATUS_PAID
      ?>
      returns
      1
      This function is defined at a runtime and can be used in control structures - if true then define('HEALTH', 'ok').
    • const
      <?php
          const STATUS_PASSED=1;
          echo STATUS_PASSED
      ?>
      It is defined during compile time, it means can't use it in loops and if /else.
  2. To find out if the constant is defined, we can use:
    <?php
        echo defined('STATUS_PASSED')
        ?>
    It returns boolean. If it returns 1, the constant is defined.
  3. Magical constants are this:
    <?php   
    echo __LINE__ (two underscores);
    ?>
    Their value depends where they are used.
  4. Variable variables $$a- takes the variable's value and treats it as the name of the new variable.
    <?php  
    $a = "capital";
    $$a
     = "city";
    echo $a.$capital  //returns "capitalcity"
    ?>
    Variable variables can be also printed out like this:
    <?php
    echo $a.$a;
    echo "$a.{$a}";
    echo "$a.${$a}"
    ?>

Learning PHP The Right Way vol 1

Notes for myself from youtube tutorial Full PHP 8 Tutorial - Learn PHP The Right Way by Program With Gio. Video nr 3: Basic PHP Syntax - PHP 8 Tutorial
  1. If the file contains only php code, there is no need to add the final
    ?>
    ending.
  2. There is no need for ending semicolon if:
    • it is the only line between php-tags
      <?php echo "Hello" ?>
    • it is the last line of the file before ending
      ?>
      tag.
  3. echo is faster than print and latter returns value 1
    <?php echo print "Hello"?>
    returns
    Hello1
  4. $this refers to object, so you can't get it as a variable's name.
  5. Variables can be sign as reference—if value changes, you change too!
    <?php 
    $x = 3;
    $z=&$x; 
    $x=1;
    echo $z
    ?>
    returns
    1

JavaScript30—Day 3

JavaScript30 is 30-day vanilla javaScript coding challenge by Wes Bos (@wesbos). The purpose of these posts is to reflect my troubles and moments of ahhaa. Lessons from Day 3 — CSS Variables + JS: Demo - Day 3
  1. - - declares variables in CSS as $ in php or var in javaScript. You declare:
    :root{
    --basecolor:#ffc600;
    }
    
    
    and call it out like this:
    p {
     backround-color: var(--basecolor);
    }
    
  2. :root is the deepest level you can go.
  3. If you want update your e.g. style attribute according to input via javaScript variable and name should have the same name, otherwise it wont work:
    
    
    - -basecolor:#ffc600;
  4. NodeList vs array document.querySelectorAll gives NodeList. The difference between NodeList and array is that array gives you all kinds and longer list of methods like map,reduce. List of methods for array:methods list for array List of methods for NodeList: methods for NodeList
Day 3 code on github.

JavaScript30—Day 2

JavaScript30 is 30-day vanilla javaScript coding challenge by Wes Bos (@wesbos). The purpose of these posts is to reflect my troubles and moments of ahhaa. Lessons from Day 2 — CSS-JS clock: Demo - Day 2
  1. Date() gives a chance to get hours, minutes, seconds etc.
    const now = new Date();
    seconds = now.getSeconds();
    minutes = now.getMinutes();
    hours = now.getHours()
    
    
    
  2. transform-origin vs transform:rotate() The first one sets the point around which the transformation takes place. Rotate() is driven by center of the element. More about MDN transform-origin.
Day 2 code on github.

Debbuging WordPress

Add following rows to wp-config.php file:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
It creates and updates debug.log file in the wp-content folder.

JavaScript30—Day 7

JavaScript30 is 30-day vanilla javaScript coding challenge by Wes Bos (@wesbos). The purpose of these posts is to reflect my troubles and moments of ahhaa. Lessons from Day 7 — Array Cardio 2: Demo - Day 7
  1. Array.prototype.some(); is there at least one match? Returns true or false.
  2. Array.prototype.every(); every single item in array matches to the condition or not? Returns true or false.
  3. console.log({variable}); - in console can be seen like
    
    isAdult:true
    
  4. Array.prototype.find(); Find is like filter, but instead returns just the one you are looking for
  5. With all these methods we can use array method without if-statment— because method returns true or false. So, instead this:
    const comment = comments.find(function(comment){
          if (comment.id === 823423){
             return true;
          }
    
        });
    
    we can write:
    const comment = comments.find(comment => comment.id ===823423);
    
  6. Deleting from array: First use Array.prototype.findIndex() and then use splice()
     const commentB = comments.findIndex(comment=>comment.id ===823423);
     //returns 1
    comments.splice(0, 1);
    
    or creating new array, spread and slice(),
    const index = comments.findIndex(comment=>comment.id ===542328);
        const newComments = [
          ...comments.slice(0,index),
          ...comments.slice(index + 1)
        ];
        console.table(newComments);
    
Day 7 code on github.

JavaScript30—Day 15

JavaScript30 is 30-day vanilla javaScript coding challenge by Wes Bos (@wesbos). The purpose of these posts is to reflect my troubles and moments of ahhaa. Lessons from Day 15 — LocalStorage and EventDelegation: Demo - Day 15
  1. Form and submit: if it is form you should listen to submit—event and not click.
  2. this.querySelector('[name=item]') So if we have form like this:
    We can grab input value like this:
    var input = document.querySelector('.js-it');
    var inputValue = input.value;
    
    or using this and parthensis:
    var input = (this.querySelector['name=it']).value;
    
  3. plates=[]: We have function
    function populateList(plates=[], plateList){
     //code here
        
      }
    
    
    plates=[] is an empty object and if we forget to pass something in, it won't broke our JavaScript
  4. .map(): creates from one array another array.
  5. .join(''): returns .map() array as a string.
  6. will still be checkedany existence of attribute checked will check it.
  7. localStorage accepts only strings. If it's something else in Console->Application-> Local Storage you'll see [object Object]. It can be stringified:
    localStorage.setItem('items', JSON.stringify(items));
    
  8. JSON. parse() is opposite to JSON.stringify(), itparses string to origin value or object.
  9. setting value as condition:
    const items = JSON.parse(localStorage.getItem('items')) || [];
    
Day 15 code on github.

JavaScript30—Day 28

JavaScript30 is 30-day vanilla javaScript coding challenge by Wes Bos (@wesbos). The purpose of these posts is to reflect my troubles and moments of ahhaa. Sorting band names without a/an/the Lessons from Day 28 — Video Speed Scrubber: Demo - Day 28
  1. This tutorial won't work with YouTube videos because you can't put embeded video into video-tag.
  2. video-tag won't play .avi-format
  3. e.pageY : MouseEvent interface returns the Y (vertical) coordinatein pixels of the event relative to the whole document
  4. element.offsetTop: is the number of pixels from the top of the closest relatively positioned parent element.
  5. element.offsetHeight: is a measurement in pixels of the element's CSS height, including border, padding and the element's horizontal scrollbar (if present, if rendered).
  6. HTMLMediaElement.playbackRate: property sets the rate at which the media is being played back. A value of 1.0 indicates normal speed and if negative—it's played back backwards. If it's too fast or slow— audio is muted.
Day 28 code on github.

Dealing with databases in Laravel vol 2 – error SQLSTATE[42000]

Error: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table users add unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes Solution:
  • In your Laravel folder go to app - Providers - AppServiceProvider.php
  • Add use Illuminate\Support\Facades\Schema; after use Illuminate\Support\ServiceProvider; If you don't do it you'll get next error: [Symfony\Component\Debug\Exception\FatalErrorException] Class 'App\Providers\Schema' not found
  • Inside boot function add next line: Schema::defaultStringLength(191);
Now your AppServiceProvider.php file should look like this:





Find solution from laravel-news.com.