Alternative Syntax For Control Statements-Modern Programing Language-Lecture Handout, Exercises of Programming Languages

Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Alternative, Syntax, Database, Function, Arguments, Php, numbers, Functions, string, Argument, Descendants

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)C# Programming
Language(Lecture 31-34) VU
Alternative syntax for control statements
PHP offers an alternative syntax for some of its control structures; namely, if, while, for,
foreach, and switch. In each case, the basic form of the alternate syntax is to change the
opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;,
endforeach;, or endswitch;, respectively. An example is shown below:
<?php
if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else: echo "a is neither 5 nor 6";
endif;
?>
User defined functions
Just like all programming languages, one can define functions in PHP as shown in the
following example:
<?php
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>
PHP also allows functions to be defined inside functions. It may be noted that C and its
descendents do not support this feature but descendants of Algol generally support it. The
following example shows this concept:
<?php
function foo() {
function bar() {
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet since it doesn't exist. */
foo();
/* Now we can call bar(), foo()'s processesing has made it
docsity.com
pf3
pf4

Partial preview of the text

Download Alternative Syntax For Control Statements-Modern Programing Language-Lecture Handout and more Exercises Programming Languages in PDF only on Docsity!

Alternative syntax for control statements

PHP offers an alternative syntax for some of its control structures; namely, if , while , for , foreach , and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif; , endwhile; , endfor; , endforeach; , or endswitch; , respectively. An example is shown below:

User defined functions

Just like all programming languages, one can define functions in PHP as shown in the following example:

PHP also allows functions to be defined inside functions. It may be noted that C and its descendents do not support this feature but descendants of Algol generally support it. The following example shows this concept:

php function foo() { function bar() { echo "I don't exist until foo() is called.\n"; } }

/* We can't call bar() yet since it doesn't exist. */

foo();

/* Now we can call bar(), foo()'s processesing has made it

accessible. */

bar(); ?>

PHP also supports recursive functions as shown below:

Function arguments

PHP supports passing arguments by value (the default), passing by reference, and default argument values. If you want an argument to a function to always be passed by reference, you can prepend an ampersand (&) to the argument name in the function definition:

You can't return multiple values from a function, but similar results can be obtained by returning a list as shown below:

To return a reference from a function, you have to use the reference operator & in both the function declaration and when assigning the returned value to a variable:

Classes and Objects

Classes and objects are similar to Java. A variable of the desired type is created with the new operator. It supports Single inheritance only and uses the keyword extends to inherit from a super class. The inherited methods and members can be overridden, unless the parent class has defined a method as final.

Databases

One of the strongest features of PHP is its support for providing web pages access to database. The following example shows database manipulation through ODBC in PHP:

"; echo "Companyname"; echo "Contactname“; while (odbc_fetch_row($rs)){ $compname=odbc_result($rs,"CompanyName"); $conname=odbc_result($rs,"ContactName"); echo "$compname"; echo "$conname"; } odbc_close($conn); echo ""; ?>