Posts

Showing posts from September, 2021

PHP array

Image
  An array stores multiple values in one single variable. In PHP, the array() function is used to create an array. Example: <?php $pets = array ( "dog" , "cat" , "rabbit" ); echo $pets [ 0 ] . "," . $pets [ 1 ] . "," . $pets [ 2 ]; ?> Output: dog,cat,rabbit An array is a special variable, which can hold more than one value at a time. If you have a list of student names, storing the names in single variables could like this, $name1 = "Peter" $name2 = "Mickle" $name2 = "Bravo" However, what if you want to loop through the names and find a specific one? And what if you had 3 names, but 1000? The solution is to create an array . An array can hold many values under a single name, and you can access the values by referring to an index number. There are three different kind of arrays. Numeric array – Arrays with a numeric index Associative array – Arrays with named keys

PHP explode function

Image
In this post, you’ll learn how to use the PHP explode() function. PHP explode() function returns an array of strings by splitting a string by a separator. The following shows the syntax of the PHP explode() function. Syntax: explode (separator, string , limit) separator - Required. Specifies where to break the string string – Required. The string to split limit – Optional. Specifies the number of array elements to return Possible Values Greater than 0 - Returns an array with a maximum of limit element(s) Less than 0 - Returns an array except for the last -limit element(s) 0 - Returns an array with one element Example: <?php $txt = "Welcome to PHP Coding Help." ; print_r ( explode ( " " , $txt )); ?> Output: Array ( [0] => Welcome [1] => to [2] => PHP [3] => Coding [4] => Help. ) Using the limit parameter to return a number of array elements: Example: <?php $str = 'red,blue,yell

Generate Secure Password Hashes

Image
PHP 5.5 introduced a simple and secure method for hashing and verifying passwords across password_hash() and password_verify() , respectively. These activities allow you to easily generate a secure hash using the most secure algorithm available to PHP. By utilizing the PASSWORD_DEFAULT option, your code will take advantage of whatever current encryption algorithm is considered most secure in that version of PHP, so there’s no need to worry about future-proofing. Syntax: <?php // Raw password, as entered by user $passwordOriginal = "leD4p?Qe5S" ; /* * Hash the password using the PASSWORD_DEFAULT algorithm. * Currently using the crypt() algorithm, but using PASSWORD_DEFAULT ensures future compatibility. * Database tables should accommodate a length of 255 characters for $passwordHash values */ $passwordHash = password_hash( $passwordOriginal , PASSWORD_DEFAULT); // Store in database. // Verify a password against the hash stored in the database with password_

PHP switch Statement

Image
The switch statement is used to perform different actions based on different conditions. Syntax: <?php switch (n) { case label1 : code to be executed if n = label1; break ; case label2 : code to be executed if n = label2; break ; case label3 : code to be executed if n = label3; break ; ... default : code to be executed if n is different from all labels; } ?> Example: <?php $favsport = "cricket" ; switch ( $favsport ) { case "cricket" : echo "Your favorite sport is cricket!" ; break ; case " football" : echo "Your favorite sport is football!" ; break ; case "hocky" : echo "Your favorite sport is hocky!" ; break ; default : echo "Your favorite sport is neither cricket, football, nor hocky!" ; } ?> Output: Your favorite sport is cricket!

PHP foreach Loop

Image
The PHP  foreach loop only works on arrays, and is used to loop through each key/ value pair in an array. Syntax: foreach ( $array as $value ) { code to be executed; } The following example will give the values of the given array ($fruits) from PHP foreach loop Example: <?php $fruits = array ( "Banana" , " Apple" , "Grapes" , "Mango" ); foreach ( $fruits as $value ) { echo $value . "<br />" ; } ?> Output: Banana Apple Grapes Mango The following example will output both the keys and the values of the given array ($age) Example: <?php $age = array ( "Kamal" => "22" , "Saman" => "35" , "Gamage" => "48" ); foreach ( $age as $x => $val ) { echo "$x = $val<br>" ; } ?> Output: Kamal=22 Saman=35 Gamage=48

Simple Validation

Image
While you can certainly create your own fancy regular expression tests to validate common strings such as an email address, IP address or URL, it is much simpler to rely on the built-in capabilities of the filter_var() function instead. By passing the appropriate FILTER_ constant, you can easily validate a wide assortment of input values with only a single line of code. Syntax: <?php // Return 'phpcodinghelper@gmail.com', a valid email address echo filter_var ( "phpcodinghelper@gmail.com" , FILTER_VALIDATE_EMAIL ); // Return '192.168.10.10', a valid IP echo filter_var ( "192.168.10.10" , FILTER_VALIDATE_IP ); // Return 'https://phpcodinghelper.blogspot.com/', a valid URL echo filter_var ( "https://phpcodinghelper.blogspot.com/" , FILTER_VALIDATE_URL ); ?>

Code for Generate Random Alphanumeric String

Image
This function is used to generate a set of random alphanumeric, you just need to pass size of code it will return alphanumeric random number. For example you pass 20 number it will return 20 digit alphanumeric random number. Syntax: <?php function generateRefCode( $num ) {      $charsAlpha = "abcdefghijklmnopqrstuvwxyz" ;      srand ((double) microtime () * 1000000 );      $i = 0 ;      $passAlpha = '' ;      while ( $i < $num / 4 ) {           $num = rand() % 33 ;           $tmp = substr ( $charsAlpha , $num , 1 );           $passAlpha = $passAlpha . $tmp ;           $i++ ;       }      $charsNum = "0123456789" ;      srand ((double) microtime () * 1000000 );      $i = 0 ;      $passNum = '' ;      while ( $i < $num ) {           $num = rand() % 33 ;           $tmp = substr ( $charsNum , $num , 1 );           $passNum = $passNum . $tmp ;           $i++ ;       }      $strReturn = $passAlpha . "-&qu