Wednesday, May 11, 2016

PHP Tutorial 2

<?php
//array
$food = array('Pasta', 'Pizza', 'Salad');
echo $food[0];
print_r ($food);

$somefood = array('Healthy'=>
array('Salad', 'Veg', 'Pasta'),
'Unhealthy'=>
array('Pizza', 'Ice cream'));
echo '<br>'.$somefood['Healthy'][0].'<br>';

foreach($somefood as $element => $inner_array){
echo '<strong>'.$element.'</strong><br>';
foreach($inner_array as $item){
echo $item.'<br>';
}
}
?>

// STRING Lower and Upper
<?php
// string function
$string = 'I Could Be Any Case.';
$str_lower = strtolower($string);
$str_upper = strtoupper($string);
echo $str_lower.'<br>';
echo $str_upper.'<br>';

if(isset($_GET['username']) && !empty($_GET['username'])){
$username = $_GET['username'];
$username_lc = strtolower($username);
if($username_lc =='alex'){
echo 'You are the best-'.$username_lc.'<br>';
}
}

?>
<form action = "some2.php" method="GET">
Name: <input type="text" name="username"><br><br>
<input type="submit" value="Submit">
</form>

// STRLEN
<?php
$find = 'is';
$offset = 0;
$find_length = strlen($find);
$string = 'This is a string, and it is an example';

// echo strpos($string, $find);
while($string_pos = strpos($string, $find, $offset)){
echo '<strong>'.$find.'</strong> found at '.$string_pos.'<br>';
$offset = $string_pos + $find_length;
}

//REPLACE
$string1 = 'This part don\'t search. This part search';
$string_new = substr_replace($string1, 'alex', 29, 4);
echo $string_new;

//REPLACE
echo '<br>';
$new_string = str_replace('is', '', $string1);
echo $new_string;
$string2 = 'This is a string, and is an example';
echo '<br>';
$finder = array('is', 'string', 'example');
$finder2 = array('IS', 'STRING', 'EXAMPLE');

$new_string1 = str_replace($finder, $finder2, $string2);
echo $new_string1;
?>

No comments:

Post a Comment