PHP Question

Status
Not open for further replies.

Kristopher

Photographer
Dec 25, 2010
803
68
So I have multiple things written in PHP and tbh it seems a bit messy, is there a way I can have a file such as phpfilename.php with all the php and I can do something like <?php echo etc... it into the page and if so how would I call it? Im new to php and just learning how to do simple tasks.
 

Benden

maging ang maganda mamatay
Jun 4, 2010
2,286
1,482
Idk what you're asking for, but you can use the include command to include files. So you put all your $thing = getfromdatabase into one file then include it in other files??
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,639
2,396
Are you on about removing code from one file to put into another and then including it so it looks like less code in your main file?

If so, you're after the include/require functions. However I wouldn't recommend it as you're just making more requests to your server.
 

Kristopher

Photographer
Dec 25, 2010
803
68
Are you on about removing code from one file to put into another and then including it so it looks like less code in your main file?

If so, you're after the include/require functions. However I wouldn't recommend it as you're just making more requests to your server.
I mean it would only called for certain pages. So like lets say profile section and news section, They would only be loaded upon them, However I want to keep my code as clean as I can get it. So basically I want all my PHP in 1 page while I include only certain parts in certain pages.
 
Last edited:

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
I mean it would only called for certain pages. So like lets say profile section and news section, They would only be loaded upon them, However I want to keep my code as clean as I can get it. So basically I want all my PHP in 1 page while I include only certain parts in certain pages.
Don't listen to Mark, it is highly recommend that you include/require different PHP files for different things, and there is absolutely no (real) impact of doing so. For very basic PHP, you'd want to create files with functions in them for specific tasks. If you're utilizing OOP, you'd be using classes.

Anyways, you should use at the top of your page to use other pages that you'd like.

PHP:
<?php

require_once 'functions/user.php';

if(isUserLoggedIn()) 
{
    echo 'Hello, ' . getUserName() . '!';
}
else
{
    echo 'Hello, Guest!';
}

You can also do things like this at a very basic level for layouts.
PHP:
<?php

require_once 'layout/top.php';

// Content goes here.

require_once 'layout/bottom.php';
 

brsy

nah mang
May 12, 2011
1,530
272
You may also be asking about classes. Check out the PHP docs for classes here
 
Status
Not open for further replies.

Users who are viewing this thread

Top