Sample PHP programs
1. Write a Hello world program using variable?
Ans:
To write the hello world program
in PHP using variable you need to know the tags that are used in HTML and the
PHP coding which is as follows:
<html>
<head><title>Hello World Script using
Variable</title></head>
<body>
<?php
$salutation = “Hello World!”;
echo “<p>$salutation</p>”;
?>
</body>
</html>
2. Write a program to show the joining of two strings in PHP?
Ans:
Two strings can be joined
together by the use of a process called as concatenation. A dot (.) operator is
used for this purpose. Example is as follows:
$string1 = _Hello_;
$string2 = _World!_;
$stringall = $string1.$string2;
echo $stringall;
3. Write down the code for save an uploaded file in php.
Ans:
if ($_FILES["file"]["error"] == 0)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" .
$_FILES["file"]["name"]);
echo "Stored in:
" . "upload/" . $_FILES["file"]["name"];
}
4. How to create a text file in php?
Ans:
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" ); exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
5. Write a program using while loop
Ans:
$my_qry = mysql_query("SELECT * FROM `users` WHERE `u_id`='1';
");
while($result = mysql_fetch_array($my_qry))
{
echo $result['First_name'.]."<br/>";
}Download PDF
<Back
EmoticonEmoticon