Skip to content

Instantly share code, notes, and snippets.

@marrek13
Last active April 21, 2022 17:50
Show Gist options
  • Save marrek13/2b48d9efc966bdd03324901e895abbc3 to your computer and use it in GitHub Desktop.
Save marrek13/2b48d9efc966bdd03324901e895abbc3 to your computer and use it in GitHub Desktop.
PHP examples
<?php
// ARRAYS
$array = [1,2,3,4,5,6];
// we need to use count function to get array length
$arrayLength = count($array);
// we need to combine 2 functions to get reversed array with elements multiplied by 2
$multipliedArrayReversed = array_reverse(array_map(fn(int $value): int => $value * 2, $array));
// take a look at filtering - array_filter has reversed function arguments compared to array_map
$oddsOnly = array_filter($array, fn(int $value): bool => $value & 1);
// STRINGS
$string = "Hello World";
// To get only second word of the sentence we can use 2 ways (there are more, but here are 2 examples)
$onlySecondWord = substr($string, strpos($string, ' ') + 1);
// OR
$onlySecondWord = explode(' ', $string)[1];
// another function to reverse the string
$reversedString = strrev($string);
// to check if the string starts with another string we use str_starts_with
$startsWithHell = str_starts_with($string, "Hell");
// but if we want to check if string contains another string arguments are in reversed order
$containsHell = str_contains("Hell", $string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment