0% found this document useful (0 votes)
6 views

CourseLecture Familiarization With String Functions in PHP

Uploaded by

jay.abaleta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CourseLecture Familiarization With String Functions in PHP

Uploaded by

jay.abaleta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Course Learning

Familiarization with string functions in


PHP
What is PHP?
• PHP stands for "PHP Hypertext Preprocessor“
• A server-side scripting language
• Used to make web pages dynamic:
 provide different content depending on context
 interface with other services: database, e-mail, etc
 authenticate users
 process form information
Life Cycle of PPHP web request
Why PHP?
• anyone can run a PHP-enabled server
free of charge
• compatible: supported by most
popular web servers
• simple: lots of built-in functionality;
familiar syntax
Basic PHP syntax
•Syntax:
• <?php (the PHP start tag)
• statement;
• ?> (the PHP end tag).

Example:
Viewing output in PHP
PHP echo and print statement

The PHP echo Statement


• The echo statement can output one or more
strings. In general terms, the echo statement
can display anything that can be displayed to
the browser, such as string, numbers, variables
values, the results of expressions.
Example:
<?php
// Displaying string of text
echo "Hello World!";
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
echo $txt;
echo "<br>";
echo $num;
echo "<br>";
echo $colors[0];
?>
PHP echo and print statement

The PHP print Statement


• You can also use the print statement (an
alternative to echo) to display output to the
browser. Like echo the print is also a language
construct not a real function. So you can also
use it without parentheses like: print or print().
Example:
<?php
// Displaying string of text
print "Hello World!";
// Displaying HTML code
print "<h4>This is a simple heading.</h4>";
print "<h4 style='color: red;'>This is heading with style.</h4>";
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
print $txt;
print "<br>";
print $num;
print "<br>";
print $colors[0];
?>
Comments in PHP?
•1. //- Single-line comment
• 2. /* ….*/ - Block Comment

Example:
<html>
<body>
<?php
//This is a comment
/*This isa commentblock*/
?>
</body>
</html>
Arithmetic Operators in PHP
+ - */%
++ --
= += -= *= /= %= .=
many operators auto-convert types: 5 + "7"
is 12
Variables in PHP
- All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or
arrays.

Syntax:
$variable_name = value;

Rules for PHP variables:


•A variable starts with the $ sign, followed by the name of the variable
•A variable name must start with a letter or the underscore character
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
•Variable names are case-sensitive ($age and $AGE are two different variables)
Example: Variable in PHP
Example#1: To create a simple PHP variable:

<?php
$age = 30;
?>

Example#2: Reassigning a variable


<?php
$age = 30;
echo $age;
?>
Data types in PHP?
• Basic types: int, float, boolean, string, array, object, NULL
test what type a variable is with is_type functions, e.g. is_string
- gettype function returns a variable's type as a string (not often needed)

PHP converts between types automatically in many cases:


string → int auto-conversion on + ("1" + 1 == 2)
int → float auto-conversion on / (3 / 2 == 1.5)

type-cast with (type):


$age = (int) "21";
String type in PHP
Example:
$favorite_food = "Ethiopian";
print $favorite_food[2];

zero-based indexing using bracket notation


string concatenation operator is . (period), not
+
5 + "2 turtle doves" === 7
5 . "2 turtle doves" === "52 turtle doves"
can be specified with "" or ''
PHP String Functions
strlen() - Return the Length of a String

The PHP strlen() function returns the length of a


string.
Example:
<?php
echo strlen("Hello Students!"); // outputs 15
?>
PHP String Functions
str_word_count() - Count Words in a String

The PHP str_word_count() function counts the


number of words in a string.
Example:
<?php
echo str_word_count("Hello Students!"); // outputs 2
?>
PHP String Functions
strrev() - Reverse a String

The PHP strrev() function reverses a string.

Example:
<?php
echo strrev("Hello Jay!"); //Output !yaJ olleH
?>
PHP String Functions
strpos() - Search For a Text Within a String

The PHP strpos() function searches for a specific


text within a string.

Example:
<?php
echo strpos("Hello Jay!"); //Output 6
?>
End of Presentation

You might also like