<i>Hello Readers! LOL</i>
Eh? what’s this you say? That ^ is not in italics? O_O
Erm.. how embarrassing! :_; lol
Here ya go:
Hello Raiders! :{P
Better, eh?
What’s with that face.. What, nothing? Hmm.. okay then, fine, onwaardss to glory! @u@!
Strangeness aside, welcome to the abyss of my splendidd and interesting mind! :D
I am currently reading three books on PhP, and am looking into content management systems like Adobe CQ5.
What the hell is that?
I am not sure, but it is pretty damn legit.
LOL
Anywaaayyyzz,
PhP is a lot like Perl, only easier to use. :P
Well… I kinda love Perl, but I like PhP too. Smexiness~*sniff, sniff* XD
The way HTML works is like you have a bunch of tags which tell the web browser how to display and format text, right?
Well PhP is a scripting language that you embed in special php html tags: <?php ?>
How this works is like so:
The server parses the page with the php in it and replaces the php code with the output from the php code.
i.e.
<?php
print “<i>I LOVE CHEESE!<i><br>”;
?>
This code would be replaced by
“<i>I LOVE CHEESE!<i><br>”
In the html file sent to the web browser of whoever is viewing your website.
Thussss, you can make websites that display different content for different users depending on certain factors, etc, make websites that accept user input and do different things based on that input, etc.
Perl does the same thing basically, but is not embedded in html.
Rather, Perl has html embedded within it.
Honestly it doesn’t bother me either which way, I just write functions to do all the html dirty work for me anyway.
And I will eventually make a perl script that can read in a file and format it how I wish.
Not really that big of a deal either way, but PhP is definitely the favored one currently, while Perl has a huge existing code base that you could draw from.
Honestly, you could probably get the two langauges to work together.
Something I am going to play around with soon I think >:D
Hehehe… ;)
Here, for example, here is a VERY small demo site I am working on in PhP.
I’ll sprinkle some comments throughout to explain what the heck is going on lol.
<!DOCTYPE html PUBLIC "-//W3C//
DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtl1/DTD/
xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/
1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html";
charset=utf-8 />
<title>Basic PHP Webpage</title>
</head>
<body>
<p>Hello world! DERP!</p>
<?php
echo "<form action=\"first.php\" method=\"post\">\n";
htmlForm("radio", "gender", "M"); #I tend to be a lazy coder, and make functions for everything. This function is used to create “input” fields/forms
htmlForm("radio", "gender", "F");
htmlForm("submit", "", "Submit");
#echo, print, both just output text to the html webpage this PhP is embedded int
echo "<br>Hello PHP world! DARP! ^u^<br>\n";
htmlPrint("Hello Johnathon!<br>", "i"); #this is an interesting function I made
#the idea is that you shouldn’t have to type <i><b><u>”Some text”</u></b></i>
#instead you just type “ibu”, and the function takes care of the rest for you
#olin just is online input, name stolen/borrowed from my CIS101 professor
#so basically if there is a form named “gender” it returns the value passed in from
#the gender form, or if there is nothing it by default returns NULL (which is also a form of false in PHP)
if( olin("gender", NULL) ){ #SO if “gender” passed in some info
#okay this is kinda sketch, bear with me…
# here I am using the “tertiary operator (?)” which is like this
# if olin(“gender”) == “M” then $message = “Male, and…”, else $message = “Female..”
# so $var = conditional ? true_result : false_result;
$message = ( olin("gender") == "M" ? "Male, and good thing too.<br>Men tend to overlook ugly websites! XD" : "Female, and why welcome Missy!<BR>Please don't judge the ugliness of this website.. I'ma working on it! I swear! XD" );
#simple print statement
print "Hello User! You said you were " . $message;
}
#These are constants, have to be defined using define() function
define(NAME, "John" ); #You can define constants like this.
define(AGE , 18 );
define(SEX , "Male" );
#These are predefined variables that store info about the environment teh script is running in
$fileurl = $_SERVER['SCRIPT_FILENAME'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$server_info = $_SERVER['SERVER_SOFTWARE'];
echo "<br>\n<br>\n<br>\n";
echo $fileurl . "<br>\n"; #IF you are confused, the . means concatenation
echo $user_agent . "<br>\n"; #i.e. joining strings together, or turning two things
echo $server_info . "<br>\n"; #into one string
echo PHP_VERSION . " kaj " #i.e. “Apple” . “ Pie” –> “Apple Pie”
. PHP_OS
. "<BR>\n";
echo "\n" . NAME . " " . AGE . " years old, " . SEX . "<BR>\n";
#Now for the functions I wrote…
#Note that I will not be putting comments in this section.
#Except for a few on Olin I think. (Oh and function names in PhP are case-insensitive.
#Olin() is olin() is oLiN() is etc etc
/*
htmlPrint isn't perfect yet, as it only does tags with one letter in their name (i.e. p, i, b, u, etc)
Due to the way it splits the input into individual characters.
Needs regular expressions, as well as looking for special html tags that don't need a twin, will fix later probably tomorrow
*/
#htmlPrint( $textToPrint, $stringOfTokenSeparatedhtmlTags, $classId
#htmlPrint("Wow, what an interesting person", "pibu", "" ) #last one is always classID I think, or we can u
#se regular expressions to find out which one contains a keytoken
function htmlPrint( $text, $html , $class = NULL ) {
if(isset($class)){
#classID not used in this version, probably will find a better way to implement it anyway
}else{
$arrayHtml = (isset($html) && strlen($html) >= 1 ? str_split($html,1) : NULL); #temporary for now, until I master regex's
$leng = count($arrayHtml); #might cause an error since I am making it NULL if $html isn't set n stuff.. lol
/*
This loop is taking all the html tags (that have no < > yet) and is putting their matching tags at the opposite side of the array
i.e. p i b </b> </i> </p>
*/
for( $i = 0; $i < $leng; $i++ ){
$arrayHtml[2*$leng - $i - 1] = '</' . $arrayHtml[$i] . ">"; #making the matching tags for the original elements
}
#make all the first tags
for( $i = 0; $i < $leng; $i++ ){
$arrayHtml[$i] = '<' . $arrayHtml[$i] . '>';
}
$leng2 = count($arrayHtml);
for( $i = 0; $i < $leng2; $i++ ){
if($i == $leng2/2){
$text2 .= $text . $arrayHtml[$i];
}else{
$text2 .= $arrayHtml[$i];
}
}
echo $text2 . "\n"; #not sure if the /n is needed or not..
}
}
#htmlForm("radio", "gallon_price", "");
function htmlForm($type, $name, $value ){
echo "<input type=\"$type\" name=\"$name\" value=\"$value\" ";
if( $type == "radio" && isset($_POST["$name"]) && $_POST["$name"] == $value ){
echo 'checked="checked"';
}
echo ($type != "submit" ? " /> $value \n" : " />\n");
}
#
function olin($name, $default = ""){ #assuming you made it here, $_POST[] is a special variable that holds the data submitted via a Post method from an Html form.
#As opposed to $_GET[], which holds data from a Get method from an Html form.
#Also note that in PhP, unlike in Perl, arrays are also considered scalars and have the $ sign
if( isset($_POST[$name]) && !empty($_POST[$name]) ){
return $_POST[$name];
}
return $default;
}
?>
</body>
</html>
Well there you have it!
A simple script hacked together in PhP!
It kinda demonstrates my laziness and the dread I feel when it comes to writing html by hand lol.
Wherever possible I try to make functions that can efficiently minimize my work and do all the repetitive stuff for me.
They are in beta form right now, but they will continue to become more powerful and sophisticated as I become better and better at programming (in general and in PhP and Perl).
Whether or not writing functions for such trivial purposes is good practice or not.. well.. I am not sure, but until my programs are pressed for memory I won’t worry too much ^u^
I’ll be working on a website for PhP and Perl soon, teaching their basics with examples and quizzes :{P
(and one for Esperanto and the like hehe XD)
Will take a long time though @o@
But those are some pet projects I am interested in pursuing. :D
Well, there is more I could talk about, but I am going to let this post come to a close lol.
Thanks for reading! :)
And code on mah brothas and sistahs! \m/_(^TuT^)_\m/
Sincerely,
~James D.
No comments:
Post a Comment