Chapter One: Server Side Scripting Basics 11/28/2019 BantamlakDejene,Information Technology 1
Introduction to Server-Side Scripting Today’s Web users expect exciting pages that are updated frequently and provide a customized experience. For them, Web sites are more like communities, to which they’ll return time and again. At the same time, Web-site administrators want sites that are easier to update and maintain, understanding that’s the only reasonable way to keep up with visitors’ expectations. For these reasons and more, PHP and MySQL have become the de facto standards for creating dynamic, database driven Web sites. 11/28/2019 BantamlakDejene,Information Technology 2
Cont.…. Dynamic Web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic Web sites Respond to different parameters (for example, the time of day or the version of the visitor’s Web browser) Have a “memory,” allowing for user registration and login, e-commerce, and similar processes Almost always integrate HTML forms, allowing visitors to perform searches, provide feedback, and so forth Often have interfaces where administrators can manage the site’s content Are easier to maintain, upgrade, and build upon than statically made sites. 11/28/2019 BantamlakDejene,Information Technology 3
Cont.…. There are many technologies available for creating dynamic Web sites. The most common are ASP.NET (Active Server Pages, a Microsoft construct), JSP (Java Server Pages), ColdFusion, Ruby on Rails (a Web development framework for the Ruby programming language), and PHP. Dynamic Web sites don’t always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL are available at little to no cost. 11/28/2019 BantamlakDejene,Information Technology 4
Server-Side Scripting Languages PHP originally stood for “Personal Home Page” as it was created in 1994 by Rasmus Lerdorf to track the visitors to his online résumé. As its usefulness and capabilities grew, it came to mean “PHP: Hypertext Preprocessor. According to the official PHP Web site, found at www.php.net A, PHP is a “widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.” It’s a long but descriptive definition. Server side refers to the fact that everything PHP does occurs on the server. A Web server application, like Apache or Microsoft’s IIS, is required and all PHP scripts must be accessed through a URL. Its cross-platform nature means that PHP runs on most operating systems, including Windows, UNIX, and Macintosh. More important, the PHP scripts written on one server will normally work on another with little or no modification. 11/28/2019 BantamlakDejene,Information Technology 5
Cont.…. MySQL (www.mysql.com) is the world’s most popular open-source database. In fact, today MySQL is a viable competitor to the pricey goliaths such as Oracle and Microsoft’s SQL Server. Like PHP, MySQL offers excellent performance, portability, and reliability, with a moderate learning curve and little to no cost. MySQL is an RDBMS. A database, in the simplest terms, is a collection of data, be it text, numbers, or binary files, stored and kept organized by the DBMS. MySQL is an open-source application, like PHP, meaning that it is free to use or even modify. There are occasions in which you should pay for a MySQL license, especially if you are making money from the sales or incorporation of the MySQL product. The MySQL software consists of several pieces, including the MySQL server, the MySQL client, and numerous utilities for maintenance and other purposes. PHP always had good support for MySQL, and that is even truer in the most recent versions of the language. 11/28/2019 BantamlakDejene,Information Technology 6
Use Basic Syntax <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Page Title</title></head> <body><!-- Example 1 - template.html --> </body></html> 11/28/2019 BantamlakDejene,Information Technology 7
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Basic PHP Page</title></head> <body><!—Example 2 - first.php --> <p>This is standard HTML.</p><?php?></body></html> 11/28/2019 BantamlakDejene,Information Technology 8
Send Data to Web Browser <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Using Echo</title></head> <body><!-- Example 3 - second.php --> <p>This is standard HTML.</p> <?php echo <p>This is generated by using PHP!</p> ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 9
Write Comments <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Comments</title></head> <body><?php # Example 4 - comments.php # Created March 16, 2011 # Created by Larry E. Ullman # This script does nothing much. echo '<p>This is a line of text.<br /> This is another line of text.</p>'; /* echo 'This line will not be executed.'; */ echo "<p>Now I'm done.</p>"; // End of PHP code. ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 10
Utilize Variables Regardless of what type you are creating, all variable names in PHP follow certain syntactical rules: A variable’s name must start with a dollar sign ($), for example, $name. The variable’s name can contain a combination of letters, numbers, and the underscore, for example, $my_report1. The first character after the dollar sign must be either a letter or an underscore (it cannot be a number). Variable names in PHP are case sensitive! This is a very important rule. It means that $name and $Name are entirely different variables. 11/28/2019 BantamlakDejene,Information Technology 11
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Predefined Variables</title></head> <body><?php # Example 5 - predefined.php // Create a shorthand version of the variable names: $file = $_SERVER['SCRIPT_FILENAME']; $user = $_SERVER['HTTP_USER_AGENT']; $server = $_SERVER['SERVER_SOFTWARE']; // Print the name of this script: echo "<p>You are running the file:<br /><b>$file</b>.</p>n"; // Print the user's information: echo "<p>You are viewing this page using:<br /><b>$user</b></p>n"; // Print the server's information: echo "<p>This server is running: <br /><b>$server</b>.</p>n"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 12
Manipulate Strings A string is merely a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. These are all strings: -> ‘Tobias’ -> “In watermelon sugar” -> ‘100’ -> ‘August 2, 2011 Make a string variable, assign a string value to a valid variable name: $first_name = 'Tobias'; $today = 'August 2, 2011'; When creating strings, you can use either single or double quotation marks to encapsulate the characters, just as you would when printing text. Likewise, you must use the same type of quotation mark for the beginning and the end of the string. If that same mark appears within the string, it must be escaped: $var = "Define "platitude", please."; Or you can also use the other quotation mark type: $var = 'Define "platitude", please.'; To print out the value of a string, use either echo or print: echo $first_name To print the value of string within a context, you must use double quotation marks: echo "Hello, $first_name"; 11/28/2019 BantamlakDejene,Information Technology 13
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # Example6 - strings.php // Create the variables: $first_name = 'ሐዲስ'; $last_name = 'አለማሁ'; $book = 'ፍቅር እስከ መቃብር'; // Print the values: echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 14
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string concatenation, length and word counting => string_concat.php // Create the variables: $favoriteAnimal = 'cat'; $myArray ['age']= '28'; $about = "My age"; $len = strlen($about); $word = str_word_count("My favorite animals are {$favoriteAnimal}s"); // Print the values: echo "My favorite animals are {$favoriteAnimal}s => There are {$word} words in this sentence</br>"; // using curly bracket echo $about . " is " . $myArray["age"] . "</br>"; // using . operator echo "The length of " . $about . " is " . $len; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 15
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string search and count => string_search.php // Create the variables: $su = 'Welcome to Samara University. Welcome to Department of Information Technology <br> This is the Department of Information Technology from College of Engineering and Technology in Samara University'; $search = strstr($su, "Engineering");// searches the string from the sentence $searchF = strpos($su, "University");// locates the string starting from beginning $searchE = strrpos($su, "Samara");// locates the string starting from end $count = substr_count($su, "Technology");//counts the repetition of the string in the text $countS = substr_count($su, "to", 9);//counts the repetition of the string in the text starting form the given index $countF = substr_count($su, "of", 45, 100);//counts the repetition of the string in the text between the indexes // Print the values: echo $su . "</br>"; echo "</br>Engineering is found at => ". $search . "</br>"; echo "University is found at => " . $searchF . " => from the beginning </br>"; echo "Samara is found at => " . $searchE . " => from the end </br>"; echo "Technology is repeated => " . $count . " => times in the text </br>"; echo "to is repeated => " . $countS . " => times in the text starting from index 9 </br>"; echo "of is repeated => " . $countF . " => times in the text between index 45 and 100 </br>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 16
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string manipulation => string_manipulation.php // Create the variables: $myString = "It was the best of times, it was the worst of times"; $string = "Here's a little string"; $str = "Information Technology"; echo $myString . "</br></br>"; echo str_replace("times", "bananas", $myString, $num ). "</br>";//replaces all and count it echo "The text was replaced $num times. </br></br>"; echo substr_replace($myString, "bananas", 11 ). "</br></br>";//replaces from the given index echo substr_replace($myString, "bananas", 19, 5). "</br></br>";//replaces from the given index to the given number of characters echo substr_replace($myString, "really", 3, 0) . "</br></br>"; echo strtr($myString, " '", "+-"). "</br></br>"; echo strtolower($str) . "</br></br>"; echo ucfirst($str) . "</br></br>"; echo lcfirst($str) . "</br></br>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 17
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><title>Type Specifiers in Action</title > <link rel="stylesheet" type="text/css" href="common.css"></head> <body><h1>Type Specifiers in Action</h1> <?php $myNumber = 123.45; printf("Binary: %b </br>", $myNumber); printf("Character: %c </br>", $myNumber); printf("Decimal: %d </br>", $myNumber); printf("Scientific: %e </br>", $myNumber); printf("Float: %f </br>", $myNumber ); printf("Octal: %o </br>", $myNumber ); printf("String: %s </br>", $myNumber ); printf("Hex (lower case): %x </br>", $myNumber ); printf("Hex (upper case): %X </br>", $myNumber ); ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 18
Manipulate Numbers Work with Constants PHP has both integer and floating-point (decimal) number types. These two types can be classified under the generic title numbers without losing any valuable distinction (for the most part). Valid number-type variables in PHP can be anything like -> 8 -> 3.14 -> 10980843985 -> -4.2398508 -> 4.4e2 Notice that these values are never quoted—quoted numbers are strings with neither numeric values— nor do they include commas to indicate thousands. Also, a number is assumed to be positive unless it is preceded by the minus sign (-). Along with the standard arithmetic operators you can use on numbers (Table 1.1), there are dozens of functions built into PHP. Two common ones are round( ) and number_format( ). The former rounds a decimal to the nearest integer: $n = 3.14; $n = round ($n); // 3 11/28/2019 BantamlakDejene,Information Technology 19
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Numbers</title></head><body> <?php # Script 1.8 - numbers.php // Set the variables: $quantity = 30; // Buying 30 widgets. $price = 119.95; $taxrate = .05; // 5% sales tax. // Calculate the total: $total = $quantity * $price; $total = $total + ($total * $taxrate); // Calculate and add the tax. // Format the total: $total = number_format ($total, 2); // Print the results: echo "<p/>You are purchasing </br>" . $quantity . "</br> widget(s) at a cost of <br>$" . $price . "</br> each. With tax, the total comes to <b>$" . $total . ".</br></p>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 20
Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Constants</title></head><body> <?php # Script 1.9 - constants.php // Set today's date as a constant: define ('TODAY', 'March 16, 2011'); // Print a message, using predefined constants and the TODAY constant: echo '<p>Today is ' . TODAY . '.</br> This server is running version <b>' . PHP_VERSION . '</b> of PHP on the <b>' . PHP_OS . '</b> operating system.</p>'; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 21
THANK YOU 11/28/2019 BantamlakDejene,Information Technology 22

server side scripting basics

  • 1.
    Chapter One: Server Side ScriptingBasics 11/28/2019 BantamlakDejene,Information Technology 1
  • 2.
    Introduction to Server-SideScripting Today’s Web users expect exciting pages that are updated frequently and provide a customized experience. For them, Web sites are more like communities, to which they’ll return time and again. At the same time, Web-site administrators want sites that are easier to update and maintain, understanding that’s the only reasonable way to keep up with visitors’ expectations. For these reasons and more, PHP and MySQL have become the de facto standards for creating dynamic, database driven Web sites. 11/28/2019 BantamlakDejene,Information Technology 2
  • 3.
    Cont.…. Dynamic Web sitesare flexible and potent creatures, more accurately described as applications than merely sites. Dynamic Web sites Respond to different parameters (for example, the time of day or the version of the visitor’s Web browser) Have a “memory,” allowing for user registration and login, e-commerce, and similar processes Almost always integrate HTML forms, allowing visitors to perform searches, provide feedback, and so forth Often have interfaces where administrators can manage the site’s content Are easier to maintain, upgrade, and build upon than statically made sites. 11/28/2019 BantamlakDejene,Information Technology 3
  • 4.
    Cont.…. There are manytechnologies available for creating dynamic Web sites. The most common are ASP.NET (Active Server Pages, a Microsoft construct), JSP (Java Server Pages), ColdFusion, Ruby on Rails (a Web development framework for the Ruby programming language), and PHP. Dynamic Web sites don’t always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL are available at little to no cost. 11/28/2019 BantamlakDejene,Information Technology 4
  • 5.
    Server-Side Scripting Languages PHPoriginally stood for “Personal Home Page” as it was created in 1994 by Rasmus Lerdorf to track the visitors to his online résumé. As its usefulness and capabilities grew, it came to mean “PHP: Hypertext Preprocessor. According to the official PHP Web site, found at www.php.net A, PHP is a “widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.” It’s a long but descriptive definition. Server side refers to the fact that everything PHP does occurs on the server. A Web server application, like Apache or Microsoft’s IIS, is required and all PHP scripts must be accessed through a URL. Its cross-platform nature means that PHP runs on most operating systems, including Windows, UNIX, and Macintosh. More important, the PHP scripts written on one server will normally work on another with little or no modification. 11/28/2019 BantamlakDejene,Information Technology 5
  • 6.
    Cont.…. MySQL (www.mysql.com) isthe world’s most popular open-source database. In fact, today MySQL is a viable competitor to the pricey goliaths such as Oracle and Microsoft’s SQL Server. Like PHP, MySQL offers excellent performance, portability, and reliability, with a moderate learning curve and little to no cost. MySQL is an RDBMS. A database, in the simplest terms, is a collection of data, be it text, numbers, or binary files, stored and kept organized by the DBMS. MySQL is an open-source application, like PHP, meaning that it is free to use or even modify. There are occasions in which you should pay for a MySQL license, especially if you are making money from the sales or incorporation of the MySQL product. The MySQL software consists of several pieces, including the MySQL server, the MySQL client, and numerous utilities for maintenance and other purposes. PHP always had good support for MySQL, and that is even truer in the most recent versions of the language. 11/28/2019 BantamlakDejene,Information Technology 6
  • 7.
    Use Basic Syntax <!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Page Title</title></head> <body><!-- Example 1 - template.html --> </body></html> 11/28/2019 BantamlakDejene,Information Technology 7
  • 8.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Basic PHP Page</title></head> <body><!—Example 2 - first.php --> <p>This is standard HTML.</p><?php?></body></html> 11/28/2019 BantamlakDejene,Information Technology 8
  • 9.
    Send Data toWeb Browser <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Using Echo</title></head> <body><!-- Example 3 - second.php --> <p>This is standard HTML.</p> <?php echo <p>This is generated by using PHP!</p> ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 9
  • 10.
    Write Comments <!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Comments</title></head> <body><?php # Example 4 - comments.php # Created March 16, 2011 # Created by Larry E. Ullman # This script does nothing much. echo '<p>This is a line of text.<br /> This is another line of text.</p>'; /* echo 'This line will not be executed.'; */ echo "<p>Now I'm done.</p>"; // End of PHP code. ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 10
  • 11.
    Utilize Variables Regardless ofwhat type you are creating, all variable names in PHP follow certain syntactical rules: A variable’s name must start with a dollar sign ($), for example, $name. The variable’s name can contain a combination of letters, numbers, and the underscore, for example, $my_report1. The first character after the dollar sign must be either a letter or an underscore (it cannot be a number). Variable names in PHP are case sensitive! This is a very important rule. It means that $name and $Name are entirely different variables. 11/28/2019 BantamlakDejene,Information Technology 11
  • 12.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Predefined Variables</title></head> <body><?php # Example 5 - predefined.php // Create a shorthand version of the variable names: $file = $_SERVER['SCRIPT_FILENAME']; $user = $_SERVER['HTTP_USER_AGENT']; $server = $_SERVER['SERVER_SOFTWARE']; // Print the name of this script: echo "<p>You are running the file:<br /><b>$file</b>.</p>n"; // Print the user's information: echo "<p>You are viewing this page using:<br /><b>$user</b></p>n"; // Print the server's information: echo "<p>This server is running: <br /><b>$server</b>.</p>n"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 12
  • 13.
    Manipulate Strings A stringis merely a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. These are all strings: -> ‘Tobias’ -> “In watermelon sugar” -> ‘100’ -> ‘August 2, 2011 Make a string variable, assign a string value to a valid variable name: $first_name = 'Tobias'; $today = 'August 2, 2011'; When creating strings, you can use either single or double quotation marks to encapsulate the characters, just as you would when printing text. Likewise, you must use the same type of quotation mark for the beginning and the end of the string. If that same mark appears within the string, it must be escaped: $var = "Define "platitude", please."; Or you can also use the other quotation mark type: $var = 'Define "platitude", please.'; To print out the value of a string, use either echo or print: echo $first_name To print the value of string within a context, you must use double quotation marks: echo "Hello, $first_name"; 11/28/2019 BantamlakDejene,Information Technology 13
  • 14.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # Example6 - strings.php // Create the variables: $first_name = 'ሐዲስ'; $last_name = 'አለማሁ'; $book = 'ፍቅር እስከ መቃብር'; // Print the values: echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 14
  • 15.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string concatenation, length and word counting => string_concat.php // Create the variables: $favoriteAnimal = 'cat'; $myArray ['age']= '28'; $about = "My age"; $len = strlen($about); $word = str_word_count("My favorite animals are {$favoriteAnimal}s"); // Print the values: echo "My favorite animals are {$favoriteAnimal}s => There are {$word} words in this sentence</br>"; // using curly bracket echo $about . " is " . $myArray["age"] . "</br>"; // using . operator echo "The length of " . $about . " is " . $len; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 15
  • 16.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string search and count => string_search.php // Create the variables: $su = 'Welcome to Samara University. Welcome to Department of Information Technology <br> This is the Department of Information Technology from College of Engineering and Technology in Samara University'; $search = strstr($su, "Engineering");// searches the string from the sentence $searchF = strpos($su, "University");// locates the string starting from beginning $searchE = strrpos($su, "Samara");// locates the string starting from end $count = substr_count($su, "Technology");//counts the repetition of the string in the text $countS = substr_count($su, "to", 9);//counts the repetition of the string in the text starting form the given index $countF = substr_count($su, "of", 45, 100);//counts the repetition of the string in the text between the indexes // Print the values: echo $su . "</br>"; echo "</br>Engineering is found at => ". $search . "</br>"; echo "University is found at => " . $searchF . " => from the beginning </br>"; echo "Samara is found at => " . $searchE . " => from the end </br>"; echo "Technology is repeated => " . $count . " => times in the text </br>"; echo "to is repeated => " . $countS . " => times in the text starting from index 9 </br>"; echo "of is repeated => " . $countF . " => times in the text between index 45 and 100 </br>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 16
  • 17.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string manipulation => string_manipulation.php // Create the variables: $myString = "It was the best of times, it was the worst of times"; $string = "Here's a little string"; $str = "Information Technology"; echo $myString . "</br></br>"; echo str_replace("times", "bananas", $myString, $num ). "</br>";//replaces all and count it echo "The text was replaced $num times. </br></br>"; echo substr_replace($myString, "bananas", 11 ). "</br></br>";//replaces from the given index echo substr_replace($myString, "bananas", 19, 5). "</br></br>";//replaces from the given index to the given number of characters echo substr_replace($myString, "really", 3, 0) . "</br></br>"; echo strtr($myString, " '", "+-"). "</br></br>"; echo strtolower($str) . "</br></br>"; echo ucfirst($str) . "</br></br>"; echo lcfirst($str) . "</br></br>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 17
  • 18.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><title>Type Specifiers in Action</title > <link rel="stylesheet" type="text/css" href="common.css"></head> <body><h1>Type Specifiers in Action</h1> <?php $myNumber = 123.45; printf("Binary: %b </br>", $myNumber); printf("Character: %c </br>", $myNumber); printf("Decimal: %d </br>", $myNumber); printf("Scientific: %e </br>", $myNumber); printf("Float: %f </br>", $myNumber ); printf("Octal: %o </br>", $myNumber ); printf("String: %s </br>", $myNumber ); printf("Hex (lower case): %x </br>", $myNumber ); printf("Hex (upper case): %X </br>", $myNumber ); ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 18
  • 19.
    Manipulate Numbers Workwith Constants PHP has both integer and floating-point (decimal) number types. These two types can be classified under the generic title numbers without losing any valuable distinction (for the most part). Valid number-type variables in PHP can be anything like -> 8 -> 3.14 -> 10980843985 -> -4.2398508 -> 4.4e2 Notice that these values are never quoted—quoted numbers are strings with neither numeric values— nor do they include commas to indicate thousands. Also, a number is assumed to be positive unless it is preceded by the minus sign (-). Along with the standard arithmetic operators you can use on numbers (Table 1.1), there are dozens of functions built into PHP. Two common ones are round( ) and number_format( ). The former rounds a decimal to the nearest integer: $n = 3.14; $n = round ($n); // 3 11/28/2019 BantamlakDejene,Information Technology 19
  • 20.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Numbers</title></head><body> <?php # Script 1.8 - numbers.php // Set the variables: $quantity = 30; // Buying 30 widgets. $price = 119.95; $taxrate = .05; // 5% sales tax. // Calculate the total: $total = $quantity * $price; $total = $total + ($total * $taxrate); // Calculate and add the tax. // Format the total: $total = number_format ($total, 2); // Print the results: echo "<p/>You are purchasing </br>" . $quantity . "</br> widget(s) at a cost of <br>$" . $price . "</br> each. With tax, the total comes to <b>$" . $total . ".</br></p>"; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 20
  • 21.
    Cont.…. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Constants</title></head><body> <?php # Script 1.9 - constants.php // Set today's date as a constant: define ('TODAY', 'March 16, 2011'); // Print a message, using predefined constants and the TODAY constant: echo '<p>Today is ' . TODAY . '.</br> This server is running version <b>' . PHP_VERSION . '</b> of PHP on the <b>' . PHP_OS . '</b> operating system.</p>'; ?></body></html> 11/28/2019 BantamlakDejene,Information Technology 21
  • 22.