Part 2 Flashcards

1
Q

Function to get the type of a variable

A

gettype($var);

prints string, integer, etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Function to dump the variable

A
var_dump($var);
prints string(len) "whole string"
or int(928783)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Function to reverse a string

A

strrev(“Hello, World!);

prints !dlroW ,olleH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Function to make a string lowercase

A

strtolower(“HeLLO”);

prints hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Function to repeat

A

str_repeat(hi, 5);

prints hihihihihi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Function to count number of times substring happens

A

substr_count($story, “like”);

prints amount of times like was in the story

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Function to create random number

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Pad a string to a certain length with another string

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

function to turn string to uppercase

A

strtoupper();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

funcitons to round (and up and down)

A

round();
ceil(); //round up
floor(); //round down

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

arrays reg and short syntax

A
$arr = array(1,2,3);
$arr = [1,2,3];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

print array

A

print_r($array);

doesn’t need echo!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

print array as a list of shit

A
implode($glue, $spices);
echo implode(", ", $num_arr);
prints 1, 2, 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Popping

A

array_pop();

takes array as argument. Removes the last element and returns it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pushing

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Shifting

A

array_shift();

removes the first element in the array and returns that value. Rest of the elements will be shifted down.

17
Q

Unshifting

A

array_unshift();
takes array as first argument. arguments added after will be added to the BEGINNING of the array. it returns the new number of the array.

18
Q

Associative Array

A

$my_array = [“name” => “bob”, “age” => 34, “occupation” => “teacher”];

19
Q

Printing Associative Arrays

A

print_r($array);

20
Q

Removing element from associative arrays

A

unset($array[“key”]);

removes that key and pointed element