Help - Search - Members - Calendar
Full Version: PHP: Hello World
Invision Power Services > Community Forums > Community Web Design and Coding
FυsE
PHP is a language that uses basic classes and functions.

Here is a demonstration on how to say "Hello World".


CODE
<?php
$text = "Hello World";
echo($text);

?>

CODE
<?php

     class hello {

     function auto_run() {
         $this->hello_world();
     }

     function hello_world() {
         echo($this->text());
     }

     function text() {
         return "Hello World";
     }

}
?>


CODE
<?php
echo("Hello World");
?>
Scyth
Great, I'm sure matt can use this in IPB 2!
Tommeh.
QUOTE(.Cole @ Jul 10 2005, 12:57 AM) *
PHP is a language that uses basic classes and functions.

Here is a demonstration on how to say "Hello World".
...
CODE
<?php
echo("Hello World"};
?>


oops, you've got a parser error there, the } should be a ), and you can also miss out the brackets as echo is a language construct, not a function original.gif
Stephen
laughing.gif
FυsE
ahahah Lol, this wasnt designed for matt, this was designed to help people who are starting to learn PHP should know the basics
Brendon Koz
Thanks for the added reference material, Cole. original.gif

As far as efficiency goes, however, creating multiple functions calls to do such a simple task might be useful (as in creating the auto_run() can then call multiple functions if updates were needed later on in the class' lifecycle), but will create more processor usage for stack calls.

Also, with efficiency in mind, since echo is a language construct (as already stated), removing the ()'s from the call will lessen the bytes required on the server (hosting space), require the PHP engine to parse that much less text, and look a bit cleaner. original.gif

^ Also for reference...
Phil Mossop
QUOTE(.Tom @ Jul 10 2005, 01:04 AM) *
oops, you've got a parser error there, the } should be a ), and you can also miss out the brackets as echo is a language construct, not a function original.gif


Here also:

CODE
echo($this->text};
_
CODE
function auto_run() {
hello_world();
}

Internal class functions must be called as $this->...
Ruben K.
note that auto_run() is not a constructor wink.gif
Rikki
QUOTE(.Cole @ Jul 10 2005, 12:57 AM) *
CODE
...
     function hello_world() {
         echo($this->text};
     }

     function text() {
         return "Hello World";
     }

}
?>


Also you are echoing $this->text which is a property but I assume you meant to call the method $this->text().
bılʞ
QUOTE(.Cole @ Jul 10 2005, 01:48 AM) *
ahahah Lol, this wasnt designed for matt, this was designed to help people who are starting to learn PHP should know the basics


It would probably help if you knew the basics beforehand. wink.gif
Wombat
If you are going to use OOP at least make the class reusable, otherwise there's little point in bothering. ermm.gif

CODE
<?php

class StringPrinter {

   var $string = "";

   function StringPrinter($string = "") {
      $this->string = $string;
   }

   function setString($string) {
      $this->string = $string;
   }

   function getString() {
      return $this->string;
   }

   function print() {
      echo $this->string;
   }

}

$sp = new StringPrinter();
$sp->setString("Hello World!");
$sp->print();
?>


How needlessly complicated can we make a "Hello World" script then? Anyone fancy doing a PHP-GTK one. rolleyes.gif
Rikki
QUOTE(Wombat @ Jul 10 2005, 01:18 PM) *
If you are going to use OOP at least make the class reusable, otherwise there's little point in bothering. ermm.gif

CODE
<?php

class StringPrinter {

   var $string = "";

   function StringPrinter($string = "") {
      $this->string = $string;
   }

   function setString($string) {
      $this->string = $string;
   }

   function getString() {
      return $this->string;
   }

   function print() {
      echo $this->string;
   }

}

$sp = new StringPrinter();
$sp->setString("Hello World!");
$sp->print();
?>


How needlessly complicated can we make a "Hello World" script then? Anyone fancy doing a PHP-GTK one. rolleyes.gif


Funny you should say that, I found a site that went from the easiest (echo "Hello World";) to the most complex which ran into a few hundred lines. I'll try and dig it up thumbsup.gif

[edit] http://www.ariel.com.au/jokes/The_Evolutio...Programmer.html biggrin.gif
Brendon Koz
QUOTE
You're all GURU HACKERS! original.gif
AJHeat34
can you do this in php?

<?php
$text="hello world!"
echo "$text";

notice the echo with no parantheses.

also when do you use print and echo? which one is better?
Dan C
Whilst it will work, echo "$text"; is much less efficient than just echo $text; original.gif

As far as print and echo go, they are both essentially the same, so it doesn't really matter.
_
echo is a tiny bit faster... I don't feel like explaining it, just search the web for 'echo vs print'.
giggsey
QUOTE(AJHeat34 @ Jul 10 2005, 05:12 PM) *
can you do this in php?

<?php
$text="hello world!"
echo "$text";

notice the echo with no parantheses.

also when do you use print and echo? which one is better?


That would give an error

Its trying to find ?>
FυsE
Sorry for the mistakes, was really tired at the time
phatmonkey
QUOTE(giggsey2 @ Jul 10 2005, 09:02 PM) *
That would give an error

Its trying to find ?>

Missing end tags don't produce any errors as far as I know. I believe some people leave them out to prevent output at the end of classes and things.
Nash12
QUOTE(Veracon @ Jul 10 2005, 07:11 PM) *
echo is a tiny bit faster... I don't feel like explaining it, just search the web for 'echo vs print'.


Because print returns a value (1 or 0) whether the "printing" was successfull or not.
Frankl.in
QUOTE(.Cole @ Jul 10 2005, 02:48 AM) *
ahahah Lol, this wasnt designed for matt, this was designed to help people who are starting to learn PHP should know the basics

Can't you just let them read Getting Started or Your first PHP-enabled page at PHP.net instead of showing buggy code?

Perhaps you could start there yourself too..... ermm.gif

Anyway, when you define a class, don't you think it should be called somewhere in the code? Because auto_run() is an IPB-thing, but you didn't tell if it's included or required by another IPB script or any other script that calls auto_run() in the 'hello' class.

A hint:

CODE
$foo = new hello;
$foo->auto_run();


Did you actually try to run that class without calling it? If so, did it work? Or is this more meant to be a tutorial about bugfixing where your first post in this topic is the example code?
Gism0
<?="Hello world!"?>
Dan C
QUOTE(Gism0 @ Jul 15 2005, 09:56 PM) *
<?="Hello world!"?>


That's not very portable tongue.gif
Mentor
Hi,

whats about this:

index.php
CODE
<?php
require( "hello.php" );
$hello = new hello();
$hello->out();
?>


hello.php
CODE
<?php

class hello
{
    var $out    = "";
    
    function hello()
    {
        

    }
    
    function out()
    {
        $this->out = "Hello ";
        
        require( "world.php" );
        
        $world = new world();
        
        $world->say_world(&$this);
        
        echo $this->out;
    }
}

?>


world.php
CODE
<?php

class world extends hello
{
    function say_world($class)
    {
        
        $class->out .= " world";
    }
}

?>
Stephen
Is this possibly the most pointless topic ever?
Dan C
No, I could name a few more pointless ones... tongue.gif
Gism0
QUOTE(Dan C @ Jul 16 2005, 11:36 AM) *
That's not very portable tongue.gif

hehe it looks cool though original.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.