Part 2 Flashcards
Function to get the type of a variable
gettype($var);
prints string, integer, etc
Function to dump the variable
var_dump($var); prints string(len) "whole string" or int(928783)
Function to reverse a string
strrev(“Hello, World!);
prints !dlroW ,olleH
Function to make a string lowercase
strtolower(“HeLLO”);
prints hello
Function to repeat
str_repeat(hi, 5);
prints hihihihihi
Function to count number of times substring happens
substr_count($story, “like”);
prints amount of times like was in the story
Function to create random number
rand();
getrandmax(); will give you the largest number that rand function can give you
rand(1,2); will return 1 or 2
rand(1,100); will return a number btwn 1 and 100 inclusive
Pad a string to a certain length with another string
str_pad ( string $input , int $pad_length [, string $pad_string = “ “ [, int $pad_type = STR_PAD_RIGHT ]] ) : string
This function returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.
$input = input string
$pad_length = length, if this is negative or the same amount as the string length, no padding will happen.
$pad_string = The pad_string may be truncated if the required number of padding characters can’t be evenly divided by the pad_string’s length.
$pad_type = Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
function to turn string to uppercase
strtoupper();
funcitons to round (and up and down)
round();
ceil(); //round up
floor(); //round down
arrays reg and short syntax
$arr = array(1,2,3); $arr = [1,2,3];
print array
print_r($array);
doesn’t need echo!
print array as a list of shit
implode($glue, $spices); echo implode(", ", $num_arr); prints 1, 2, 3
Popping
array_pop();
takes array as argument. Removes the last element and returns it
Pushing
array_push();
takes array as FIRST argument any argument added after will be added to the END array. returns the new number of the array