Category Archives: PHP

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

Writing into the file in PHP

The code below overwrites existing content in a file:
$handle = fopen($filename, 'w') or die('Cannot open file:  '.$filename);
The code below adds new line to the existing content:
$handle = fopen($filename, 'a') or die('Cannot open file:  '.$filename);
So, the difference is in fopen($filename, 'w') and fopen($filename, 'a')

Check if rgb(a) or hex color is in correct format

function color_format($i){
        
$pattern = "/^rgb/i";
$pattern2 ="/a/i";
 $i = strtolower($i);
//its rgb
if (preg_match($pattern, $i)){
  
      // is ending with ; 
  $no_need = substr( $i, -1 );
      // is ending with ;
    if($no_need==";") {
      $i = substr($i,0,-1);
       
    } 
    // let's split "("
       $needed_piece = explode("(", $i);
  
  //it's rgb
      if (preg_match($pattern2, $i)){
         $needed_color = "rgba(".$needed_piece[1];
      } else {
         $needed_color = "rgb(".$needed_piece[1];
      }
     
       return $needed_color;

} else{
    
   // is ending with ;
    $no_need = substr( $i, -1 );

    if($no_need==";") {
      $i = substr($i,0,-1);
       
    } 
    
    // is starting with #
 $i= ltrim($i, '#');
 if (
          ctype_xdigit($i) &&
          (strlen($i) == 6 || strlen($i) == 3))
           return $needed_color = "#".$i;


  else return false;
    

}

} 

Month’s first and last day

How to get month's first and last day in case you have your own variables? $month = 5; $full_year = 2016; For first day we can use: date("01.$month.$full_year"); // 01.05.2016 And for the last year: date("t.$month.$full_year"); // 31.05.2016