Constants in PHP Code-Programming Environment-Lab Task, Lecture notes of Programming Languages

Prof. Gaurav Bhatt assigned this lab task for Environment and Languages for Programming course at Institute of Company Secretaries of India - ICSI. Its main parts are: PHP, Code, Terminal, Diirectory, Script, Command, File, Permissions, Web, Page, Execute

Typology: Lecture notes

2011/2012

Uploaded on 07/16/2012

sambandan
sambandan 🇮🇳

4.7

(3)

35 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
<ht ml >
< he ad > < ti tl e > He l l o Wo r ld </ ti tl e > </ h ea d >
<bo dy >
<p > O u r f ir s t P H P sc r ip t </ p >
<? p hp
$u se r = "{ y ou r n am e }" ;
p ri n t (" < p > < b > H el l o $u se r < b r / > \ nH e l lo W or l d ! </ b > < /p >\ n " );
?>
</ b od y > </ h tm l >
Replace {your name} with your own name.
b. Save the code to a file named phpDemo1.php in $HOME/public_html/.
1
docsity.com
pf3
pf4

Partial preview of the text

Download Constants in PHP Code-Programming Environment-Lab Task and more Lecture notes Programming Languages in PDF only on Docsity!

< html > < head > < title > Hello World </ title > </ head > < body >

Our first PHP script Hello $user < br / >\ nHello World!

\ n "); ? >

Replace {your name} with your own name.

b. Save the code to a file named phpDemo1.php in $HOME/public_html/.

c. Open a terminal, go to the directory in which the file has been stored and make the file readable for everyone by using

chmod a+r $HOME/public_html/phpDemo1.php

You will only have to do so once. File permissions should not change while you con- tinue to edit the file. d. Execute the PHP script by using the command php ./phpDemo1.php in the terminal. Check that there are no syntax errors and that the script produces the output < html > < head > < title > Hello World </ title > </ head > < body >

Our first PHP script

Hello { your name } < br / > Hello World!

(Obviously, with your name appearing instead of {your name}.) e. Now open a web browser and access the URL

http://www.csc.liv.ac.uk/∼{user}/phpDemo1.php

where {user} should be replaced by your departmental user name. Make sure that the web page you are shown corresponds to the HTML code you have seen in 1c. above. Hint: Your web browser can shown you the HTML source of a web page. f. It is good practice to add information about who created a script/web page, who claims copyright and when a script/web page was created. Add these three pieces of information to the script using META declarations. See

http://www.w3.org/TR/html4/struct/global.html#h-7.4.4.

for details.

  1. The following example deals with the use of constants in PHP code.

a. Add the following PHP code to the end of the current PHP code in phpDemo1.php: define (" PI " ,3.14159); define (" SPEED_OF_LIGHT " ,299792458 , true ); print "1 - Value of PI : PI < br / >\ n "; print "2 - Value of PI : ". PI ." < br / >\ n "; $diameter = 2; $time = 3; $circumference1 = PI * $diameter ; $circumference2 = pi * $diameter ; $distance = speed_of_light * $time ; echo " Diameter = $diameter = > " , " Circumference1 = $circumference1 | " , " Circumference2 = $circumference2 < br / >\ n "; echo " Time = $time = > Distance = $distance < br / >\ n ";

Be careful where exactly you place the code!

echo " < tr >"; printf (" < td >%20 s == %20 s -> %5 s </ td >" , $val_sa , $val_sb , ( $sa == $sb )? " TRUE " : " FALSE "); printf (" < td >%20 s === %20 s -> %5 s </ td >" , $val_sa , $val_sb , ( $sa === $sb )? " TRUE " : " FALSE "); printf (" < td >%20 s != %20 s -> %5 s </ td >" , $val_sa , $val_sb , ( $sa != $sb )? " TRUE " : " FALSE "); printf (" < td >%20 s !== %20 s -> %5 s </ td >" , $val_sa , $val_sb , ( $sa !== $sb )? " TRUE " : " FALSE "); } echo " </ table >\ n ";

b. Save the file and execute the script using the web browser. Analyse the table that the additional code produces and make sure that you understand its contents. Also make sure that you understand how the code above produces that table. (Generating tables is typical task for which PHP code is used.)

  1. The following example deals with the modification of array elements , string operations and pattern search and replace.

a. Add the following PHP code to the end of the current PHP code in phpDemo1.php: $names = array (" Andrew Craig " , " Dr Mingyu Guo " , " Dr Ullrich Hustadt " , " Dr Vladimir Sazonov " , " Dr Michele Zito "); // Your own code here foreach ( $names as $name ) print (" Name : $name < br / >\ n ");

b. Save the file and execute the script using the web browser. The output produced by the code above should be as follows: Name : Andrew Craig Name : Dr Mingyu Guo Name : Dr Ullrich Hustadt Name : Dr Vladimir Sazonov Name : Dr Michele Zito

c. Add code at the point indicated in 5a. that modifies the strings stored in $names so that the output produced will change to: Name : CRAIG , Andrew Name : GUO , Mingyu Name : HUSTADT , Ullrich Name : SAZONOV , Vladimir NAME : ZITO , Michele This should be done with the help of string manipulations operations, see, for example, http://www.php.net/manual/en/function.preg-replace.php http://www.php.net/manual/en/function.preg-replace-callback.php and the code should be independent of the actual names stored in $names. Hints: Recall from the lecture notes (Lecture 6, Slide 5) how this was done in Perl. Also recall from the lecture notes the variation of a foreach-loop that allows you to modify the elements of an array.