docs:programming:hello_world_examples

Hello World Examples

PHP can be embedded into an html file, and interpreted at runtime for dynamic web page content. It can also be executed at the command line by using the php program.

<?php
	echo "Hello World!";
?>
<?php
	$myString = "Hello World!";
 
	function printMyString(){
		global $myString;
		echo $myString;
	}
 
	printMyString();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<title>Hello World - ASP.NET</title>
<script runat="server" language="VB">
	Sub Page_Load(sender As Object, e As EventArgs)
		timeLabel.Text = DateTime.Now.ToString()
	End Sub
</script>
	</head>
	<body>
		<p>Hello World!</p>
		<p>The time is now: 
			<asp:Label runat="server" id="timeLabel" /></p>
	</body>
</html>

HTML by itself does not have functions or variables. It is a tagged language most commonly used to display a web page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Hello World Example</title>
 
</head>
<body>
	<h1>Hello World Example</h1>
	<p>Hello World!</p>
</body>
</html>

C is a procedural, structured language that does not have objects. C is extended by other languages, such as C++ and Objective-C.

#include <stdio.h>
 
int main(){
        printf("Hello World!\n");
        return 0;
}
#include <stdio.h>
 
const char * myString;
 
void printMyString(){
	printf("%s\n",myString);
	return;
}
 
int main(int argc, char **argv){
        myString = "Hello World!";
        printMyString();
        return 0;
}

Objective-C is an extension of c, so a simple version will not be shown here.

#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
 
@interface Hello:NSObject{
    NSString *myString;
}
-(void)printMyString;
@end
 
@implementation Hello
 
-(void)printMyString{
    myString = [NSString stringWithString:@"Hello World!"];
    NSLog(myString);
}
@end
 
int main( int argc, const char *argv[] ) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];	
    Hello *h = [[Hello alloc] init];
    [h printMyString];
    [pool release];
    return 0;
}

* At least on the Mac platform, use g++ instead of gcc to compile because it already has the C++ standard libraries linked.

#include <iostream>
 
int main()
{
	std::cout << "Hello World!\n";
	return 0;
}
#include <iostream>
#include <string>
 
using namespace std;					// or type std:cout everywhere
 
class Hello
{	
	public:
		Hello(const char * userString);		// Constructor
		~Hello();				// Destructor
		void printMsg();
 
	private:
		string privateString;			// private variable
 
};
 
Hello::Hello(const char * userString)
{
	privateString = userString;
}
 
Hello::~Hello()
{
	// do nothing special
}
 
void Hello::printMsg()
{
	cout << privateString;
}
 
int main()
{
	Hello HelloInstance("Hello World!\n");		// create a new instance
	HelloInstance.printMsg();
	return 0;
}
public class HelloWorld{
        public static void main(String args[]){
                System.out.println("Hello World!");
        }
}
public class HelloWorldVar{
 
        private String myString = "Hello World!";
 
        private void printMyString(){
                System.out.println(myString);
        }
 
        public static void main(String args[]){
                HelloWorldVar hw = new HelloWorldVar();
                hw.printMyString();
        }
}
#!/usr/bin/perl
 
print "Hello World!\n";
#!/usr/bin/perl -w               # the 'w' tells perl to inform you of any
                                 # warnings encountered
 
#use strict;                     # tell perl to impose strict verification,
                                 # and output additional warnings;
                                 # it is commented out here because it will
                                 # fail since the variable below requires
                                 # an explicit package name (such as my)
 
$myString = "Hello World!";      # $myString is a global variable
 
sub printMyString {              # sub is used to define a subroutine, or
                                 # function in perl;
 
  print $myString . "\n";        # this instance of $myString references
                                 # the global $myString variable;
}
 
sub printLocalString {
  my $myString = "Hello Moon!";  # the my operator marks this instance of
                                 # $myString as private;
 
  print $myString . "\n";        # this statement is using the private
                                 # $myString above;
}
 
printMyString();                 # call a subroutine
 
printLocalString();              # call a subroutine
  • docs/programming/hello_world_examples.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1