Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
╰┈➤ The eval() function evaluates a string as PHP code. The string must be valid PHP code and must end with
semicolon.
// Example #1 eval() 
$string = 'cup';
$string2= 'coffee';
$str = 'This is a $string with $string2 in it.';
echo $str. "\n"; // outputs: This is a $string with $string2 in it.
eval("\$str = \"$str\";");
echo $str. "\n"; // outputs: This is a cup with coffee in it.
► Let's make another simple example for better understanding. Put a mathematical operation in a string. obviously PHP can't solve example in case of putting it in a string.◄
$operation =  "20 - 5"; 
echo $operation; // outputs 20 - 5
echo eval( $operation. ';' );  // it doesn't show anything.
echo eval( 'echo '. $operation. ';' ); // outputs 15
// or
eval( '$temp = ' .$operation. ';' );
echo $temp; // outputs 15
by Luka Khitaridze
2 years ago
0
PHP
Function
0
╰┈➤ Create a function that determines whether a string is a valid hex code. The hex code must begin with a pound key # and is exactly 6 characters in length. Each character must be a digit from 0-9 or an alphabetic character from A-F. All alphabetic characters may be uppercase or lowercase. ◄
function isSpecialArray($str) {
    $length = strlen($str);
    $count=1;
    for ($i=1; $i<$length; $i++) {
	if ($str[0]="#") {
	    
► // 0123456789abcdefABCDEF
if ((ord($str[$i]) > 64 && ord($str[$i]) < 71) || (ord($str[$i]) > 47 && ord($str[$i]) < 58) || (ord($str[$i]) > 96 && ord($str[$i]) < 103)) { $count+=1; } } } if (strlen($str)==7 && $count==$length){ return true; } return false; } var_dump(isSpecialArray("#CD5C5B"));
`
by Luka Khitaridze
2 years ago
0
PHP
Problem solving
0
╰┈➤ Create a function that accepts a string, checks if it's a valid email address and returns either
true 
or
false
function validateEmail($email) {
    if ($email == "") {
	return false; "\n";
    }
    $reversed = strrev($email);
    $position = strpos($email, "@");
	if (substr_count($email, "@") == 1 && $position > 0 && ord($email[$position + 1]) > 96 
            && ord($email[$position+1])<123 && substr_count($email, ".") > 0 
            && strpos($reversed, ".") < strpos($reversed, "@") - 1 && $reversed[0] != ".")  {
	        return true; "\n";
	        }
    return false; "\n";
}
echo(validateEmail('@edabit.com')); //false
echo(validateEmail('bill.gates@microsoft.com'));//true
by Luka Khitaridze
2 years ago
0
PHP
Problem solving
1

<?php 
function gettingmarried($women, $men) {

$count=count($women); //count-counts the number of women.

// '!=' -If not equal.
if ($women != count($men)) {
echo "I don't have all pairs";
}

//array_combine-Pairs the data of the first element with the second.
return  array_combine($women, $men);
}

print_r(gettingmarried(["elene","mariami"], ["dato", "vano"]));
by Gvanca Gharibashvili
2 years ago
0
PHP
Object Oriented PHP Tutorial
0

<?php
function space($sentence) {

$sentence= trim($sentence); //trim function-Breaks a sentence after spaces.

//str_contains-Checks if there is a blank in this sentence in this situation.
while (str_contains($sentence, '  ')) {

// str_replace-Replaces the first element with the second element in this sentence.
$sentence= str_replace('  ', ' ',$sentence);
}
return $sentence;
}
$sentence "  hi     i    am   gvanca";
echo space($sentence);
by Gvanca Gharibashvili
2 years ago
0
PHP
Object Oriented PHP Tutorial
0
isPalindrome
 <?php
  function isPalindrome($s) {
        //s = "A man, a plan, a canal: Panama"
        $s= strtolower($s);
         $filtered = "";
        $len = strlen($s);
        for ($i= 0; $i<$len; $i++){
            $ascii = ord($s[$i]);
            if (($ascii>=97 && $ascii<123) ||
             ($ascii>47 && $ascii<58)){
                $filtered.=$s[$i];
                }
        }
        return $filtered == strrev($filtered);
    }
   $s = "A man, a plan, a canal: Panama";
   echo isPalindrome($s);
by Guram Azarashvili
2 years ago
0
PHP
Solutions
0
628 max product of 3 numbers
<?php function maximumProduct($nums) { $count=count($nums); sort($nums); //for all positive numbers-$prod1: $prod1=$nums[$count-1]*$nums[$count-2]*$nums[$count-3]; //for negative numbers-$prod2: $prod2=$nums[0]*$nums[1]*$nums[$count-1]; return max($prod1, $prod2); } echo maximumProduct([7,2,1,2,3,4])."\n"; echo maximumProduct([-1,-2,-3,1,2,3,4])."\n";
by Anna Tukvadze
2 years ago
0
PHP
0
╰┈➤The preg_match() function returns whether a match was found in a string, preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or false on failure (error).
$string = "hello world";
$pattern = "/world/";
echo preg_match($pattern, $string);
// outputs 1
► As usual, we use this function together with Regex (Regular Expression) syntax and we adapt it to the different solutions. for example, in regular expression "i" means case insensitive check. ◄
therefore if that case would be like that:
$string = "Hello wORLD";
$pattern = "/world/i";
echo preg_match($pattern, $string);
// it would still outputs 1
► Valeri has used this function in a task to check for the only uppercase letters, only lowercase letters and first uppercase letter in the string via regex. ◄
For example: (via Regex - Regular Expression and preg_match)
(^[A-Z]*$) it means if there are only uppercase letters in a string.
(^[a-z]+$) it means if there are only lowercase letters in a string.
(^[A-Z]{1}[a-z]*$) it means if the first letter is uppercase in a string.
| - it means "Logical or" via regex as well.
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
╰┈➤ array_fill_keys() function fills an array with values, specifying keys.
function arrayFillKeys($array) {
    print_r($array);
    $array = array_fill_keys($array, 'value');
    return ($array);
}
print_r(arrayFillKeys([4,3,2]));
// testing print_r outputs: ( [0] => 4, [1] => 3, [2] => 2 ) // The function returns: ( [4] => value, [3] => value, [2] => value )
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
╰┈➤ The array_keys() function returns an array containing the keys.
function arrayKeys($array) {
    print_r($array);
    return array_keys($array);
}
print_r(arrayKeys(["House" => "White", "Car" => "Black"]));
// testing print_r outputs: ( [House] => White, [Car] => Black ) // The function returns: ( [0] => House, [1] => Car )
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
Results: 1580