PHP Strings and Functions
PHP Strings and Functions
- sequence of letters, numbers, special characters and arithmetic values or combination of all
- simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks (')
- double quotation marks (") can also be used, however, single and double quotation marks work in different ways
- strings enclosed in single-quotes are treated almost literally
- strings delimited by the double quotes replaces variables with the string representations of their values as well as
specially interpreting certain escape sequences
// Outputs: 28
echo strlen($my_str);
?>
str_word_count()
- counts the number of words in a string
<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';
// Outputs: 9
echo str_word_count($my_str);
?>
str_replace() - replaces all occurrences of the search text within the target string
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
<?php
$my_str = 'You can do anything, but not everything.';
2 types
1. Built – in Functions
2. User – defined functions
Note: The declaration of a user-defined function start with the word function, followed by the name of the function you want to create
followed by parentheses i.e. () and finally place your function's code between curly brackets {}. A function name can start with a letter
or underscore (not a number).
Tip: Give the function a name that reflects what the function does! Function names are NOT case-sensitive.
Example:
<?php
// Defining function
function whatIsToday(){
echo "Today is " . date('l', mktime());
}
// Calling function
whatIsToday();
?>
You can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder
variables within a function; they're replaced at run time by the values (known as argument) provided to the function at the time of
invocation.
function myFunc($oneParameter, $anotherParameter){
// Code to be executed
}
You can define as many parameters as you like. However for each parameter you specify, a corresponding argument needs to be
passed to the function when it is called.
The getSum() function in following example takes two integer values as arguments, simply add them together and then display the
result in the browser.
Example:
<?php
// Defining function
function getSum($num1, $num2){
$sum = $num1 + $num2;
// Calling function
getSum(10, 20);
?>
The output of the above code will be:
Sum of the two numbers 10 and 20 is : 30
Tip: An argument is a value that you pass to a function, and a parameter is the variable within the function that receives the argument.
However, in common usage these terms are interchangeable i.e. an argument is a parameter is an argument.
// Calling function
customFont("Arial", 2);
customFont("Times", 3);
customFont("Courier");
?>
As you can see the third call to customFont() doesn't include the second argument. This causes PHP engine to use the default value for
the $size parameter which is 1.5.
$mynum = 5;
echo $mynum; // Outputs: 5
selfMultiply($mynum);
echo $mynum; // Outputs: 25
?>
By default, variables declared within a function are local and they cannot be viewed or manipulated from outside of that function, as
demonstrated in the example below:
Example:
<?php
// Defining function
function test(){
$greet = "Hello World!";
echo $greet;
}
// Defining function
function test(){
echo $greet;
}
Reference: www.tutorialrepublic.com, www.w3schools.com
test(); // Generate undefined variable error
Tip: It is possible to reuse the same name for a variable in different functions, since local variables are only recognized by the function
in which they are declared.
// Defining function
function test(){
global $greet;
echo $greet;
}
/*
Loop through array, if value is itself an array recursively call the
function else add the value found to the output items array,
and increment counter by 1 for each value found
Reference: www.tutorialrepublic.com, www.w3schools.com
*/
foreach($arr as $a){
if(is_array($a)){
printValues($a);
} else{
$items[] = $a;
$count++;
}
}