×
          
              
          
      
      Clear all filters including search bar
          
        Valeri Tandilashvili's Personal Professional Blog
      
    strlen() returns the length of the string
echo strlen("Hello world!")."\n"; // 12
Function strrev() returns the Reverse of the string
echo strrev("Hello world!")."\n"; // !dlrow olleH
Function strpos() tries to find the second parameter and returns its index
echo strpos("Hello world!", "world")."\n"; // 6
Function str_replace() replaces the word "Hello" with "Hi"
echo str_replace("Hello", "Hi", "Hello there")."\n"; // Hi there
Function substr() returns the substring between given indexes
echo substr("Hello world", 6)."\n"; // world
echo substr("Hello world", 6, 3)."\n"; // wor
echo substr("Hello world", -5)."\n"; // world
echo substr("Hello world", -4, 2)."\n"; // or
Function trim() removes whitespaces from sides of the string
echo trim("   Hello World  ").".\n"; // Hello World.
// Removes whitespaces from the right end
echo rtrim("   Hello World  ")."\n"; //    Hello World
// Removes Exclamation mark from the right end
echo rtrim("Hello World!", "!")."\n"; // Hello World
Function strtoupper() converts the string to upper case
echo strtoupper("Hello World")."\n";
Function strtolower() converts the string to lower case
echo strtolower("Hello World")."\n";
Function explode() breaks the string by its delimiter
print_r (explode(" ", "It's the first day of the course!"));
Function implode() joins the array element based on the delimiter and returns the string
echo implode(" ", ['Hello', 'World']);$variable, $Variable and $VARIABLE are completely different// lowercase
$variable = "Value 1";
// PascalCase
$Variable = "Value 2";
// UPPERCASE
$VARIABLE = "Value 3";
echo $variable . "\n";
echo $Variable . "\n";
echo $VARIABLE . "\n";
Function names are case-insensitive
function printGreeting() {
	print "Prints: Hello world!\n";
}
printGreeting();Calling the function with uppercase will still workhoisted(); // logs "foo"
function hoisted() {
  console.log('foo');
}
Function expressions - anonymous functions are not hoisted:notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function() {
   console.log('bar');
};map function calls getNames function for each item of the array.
Each time the function getNames returns only names from each objectlet myFavoritePlanets = [
    {name: 'Earth', radius: 6371}, 
    {name: 'Jupiter', radius: 69911}, 
    {name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
let namesOnly = myFavoritePlanets.map(getNames);
function getNames(planet) {
  return planet.name
}
console.log(namesOnly);logError function and passes details of the error, such as: 
file, line, code, message and trace try {
    undefinedFunction();
} catch (Error $e) {
    logError(
		'ERR FILE: ' . $e->getFile() . "\n" .
		'ERR LINE: ' . $e->getLine() . "\n" .
		'ERR CODE: ' . $e->getCode() . "\n" .
		'ERR TEXT: ' . $e->getMessage() . "\n" .
		'ERR TRACE String: ' . $e->getTraceAsString()
	);
}
The function logError appends current date and time to the error details and logs into the error filefunction logError($error) {
	
	$content = "\n\n\n".date('Y-m-d H:i:s');
	$content .= "\n".$error;
	
	file_put_contents('/var/log/errors.txt', $content, FILE_APPEND);
}let myString = 'Some String'
The string variable has access to all string functions document.write(myString.toUpperCase());The result will be:SOME STRING
But if we let number variable to use the string function toUpperCase() let myNumber = 55
console.log(myNumber.toUpperCase());Then we will get the following error. This is because number type variable does not have access to string functionsUncaught TypeError: myNumber.toUpperCase is not a functionaddEventListener creates the specified event listener for the object.
In this case we add click event for the div element with id div_element without using anonymous function.
First we select the object that we want to create click event listener tolet div = document.getElementById('div_element');
Then we create the actual event listener for the event without anonymous functiondiv.addEventListener('click', func);
function func() {
    alert('You clicked me!');
}
The same example using anonymous functiondiv.addEventListener('click', function(){
  alert('You clicked me!');
})
HTML part of the examples:<div id="div_element">Click Me</div>
Another method to add the event listener is to include onclick attribute inside opening tag<div onclick="func()">Click Me</div>boyfriend inside cat objectlet cat = {
    name: 'Lucy',
    age: 4,
    meow() {
        console.log('Meowwww');
    },
    boyfriend: {
        name: 'Jack',
        age: 5,
        favoriteColors: ['Green', 'Yellow'],
        meow() {
            console.log('Meeoowwwwwwwwwwww');
        },
    }
}
Accessing nested object's attributes and methods:console.log(cat.boyfriend.name); // Jack
cat.boyfriend.meow(); // Meeoowwwwwwwwwwwwcat combines attributes and behaviors of a cat in the following objectlet cat = {
    age: 4,
    name: 'Bella',
    meow() {
        console.log('Meowwww');
    }
}
Instead of the object, we would have separated function and variables, which is not organized waylet catName = 'Bella';
let catAge = 4;
function meow() {
    console.log('Meowwww');
}green background colordocument.body.style.backgroundColor = 'green';
Sets display property to inline-block for the element with id element_id document.getElementById('element_id').style.display = 'inline-block';