...

View Full Version : Displaying PHP codes without DB yet



mpacaon
07-31-2008, 07:45 AM
Hi!

Sorry for the total noob question. I just would like to ask if its possible to view my PHP codes (even just the simple "Hello World" in echo) even if I don't have database yet.

My PHP codes are inside/saved as .html and as follows:



<html><head><title>Untitled Document</title></head>
<body>

<?php
/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$name = "Em";
$age = 19;
$school = "De La Salle University";

/* Output text using echo */
echo "Hello World";
// Output text using print
print "Hello " . $age . " years old " . $name ".";
echo "Your name has " . strlen($name) . " letters."; // Em: length = 2
echo "The word \"University\" is found at the " . strpos($school, "University") . " th position." // University: starting position = 12 (because counting starts at 0)
?>

</body>
</html>

Fou-Lu
07-31-2008, 08:16 AM
You don't need a database to program in PHP. But unless you're host is set up to parse .html files as PHP you won't get any processing either. Rename .html to .php and it should work.

mpacaon
07-31-2008, 08:29 AM
Okay, so I now changed the extension from .html to .php. However, if I try to view the .php file, nothing will be displayed in the browser? Do I need to setup something with regards to Apache/MySQL before being able to view my "Hello World"? (^_^);

kuse50
07-31-2008, 08:43 AM
just use XAMPP or WAMP (for Windows)
and put your code in htdocs (for XAMPP) or www (fror WAMP)

open ur browser n type http://localhost/path_to_your_file.php

it should work...

mpacaon
07-31-2008, 09:07 AM
I tried putting myfile.php inside Apache's htdocs, and restarted the Apache server.
I went to http://localhost/myfile.php and nothing is displayed.

I then realized that some parts of the codes are wrong:



$message = "Hello";
$number = 143;
$file = fopen("test.txt", "w");
echo vfprintf($file, "&#37;s world. Day number %u", array($message, $number));


What is actually wrong with these lines of code?

My editor (Adobe Dreamweaver CS3) won't also able to detect vfprintf as a valid function. Why is that?

Fou-Lu
07-31-2008, 10:17 PM
vprintf is the same as printf except it wants an array of parameters. You have given it three arguments, and it only accepts two arguments. You cannot write to a file in this way using PHP, and I don't believe you can override the stdout to pipe it to a file. Instead, use vsprintf/sprintf with you're format and arrays, capture the output in a variable and use fwrite to write the new entry to a file.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum