Dynadot

[Resolved] PHP Help

Spaceship Spaceship
Watch
Impact
0
PHP Help

First of all I'm new to php and I need some text to be centered using php code. How do I go aobut doing that? Thanx in advance.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Welcome to PHP :)

You wouldn't center text in PHP, it would still be done in HTML, just being sent to the browser using the php print() construct:

PHP:
<?php
print "<div style='text-align:center'>This text will be centered</div>";
?>

If you are looking for some PHP tutorials, head over to http://www.webjuniors.com/showthread.php?t=249 - quite a few tutorials I have written, I try to add one every couple of days :)
 
0
•••
Don't count on using style attributes inside div tags; not all platforms support this (think IE on pocket PC for one). It won't validate as anything but html 4.01 transitional or lower, but you can use this:
PHP:
<? print '<div align="center">Centered Text</div>' ?>
.
 
0
•••
monaco said:
Don't count on using style attributes inside div tags; not all platforms support this (think IE on pocket PC for one). It won't validate as anything but html 4.01 transitional or lower, but you can use this:
PHP:
<? print '<div align="center">Centered Text</div>' ?>
.
IF you use the adove, remember to add a ; after the ' :)

PHP:
  <? print '<div align="center">Centered Text</div>'; ?>
 
0
•••
Hitch said:
IF you use the adove, remember to add a ; after the ' :)

PHP:
  <? print '<div align="center">Centered Text</div>'; ?>

It is not actually necessary in this case as there is a closing php tag (although it is advised)

Try it and see the following is perfectly valid php:-

PHP:
<? print '<div align="center">Centered Text</div>' ?>   
<? print '<div align="center">Even More Centered Test</div>' ?>
 
0
•••
I'll do ya one better :) If all you're doing is printing something out and you're using short tags (as you are above), you can also dispense with print/echo and just use the '=' character:

PHP:
<?='<div align="center">Centered Text</div>'?>
 
0
•••
Or you could just use:
PHP:
<div align="center">Centered Text</div>
Because you don't need PHP for that.
 
0
•••
0
•••
Don't use short tags.

Use <?php

Also, make sure you add an ; after:

<?php echo 'test'; ?>

Also, you don't need php for straight HTML, it parses slower.
 
0
•••
Don't use PHP - it adds processing time.

Always use HTML when possible.
 
0
•••
music_man said:
Don't use short tags.

Use <?php

Also, make sure you add an ; after:



Also, you don't need php for straight HTML, it parses slower.

Most of that is very true, you should use standard php tags (<?php ?>) as it avoids conflict and it is the only 1 guaranteed to work (the others can be disabled or are deprecated and can be removed at any time).

And you are correct extra processing time will be taken up if you have non php in php tags as it will go through the php engine for no reason.

Regarding the ; it is not necessary in the above example as the ?> comes directly after the command (I double checked and it worked 100%). However of course best practice always dictates the use of it.
 
0
•••
True enough. If you want another echo without using ; e.g.

<?php
echo 'test'
echo 'hi'
?>

That comes up with a parse error.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back