Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The 5 biggest PHP newb coding mistakes

DZone's Guide to

The 5 biggest PHP newb coding mistakes

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

We were all PHP programming newbs at one point and we all had our own flavor of writing horrible code, but I think there are a few mistakes that pretty much all new PHP programmers will make. This is not meant to insult new coders (in fact these code examples are from some of my first websites), the goal is to help new programmers identify poor practices so they can improve and land that next big gig. 


1. Destroying Memory Usage 

PHP absolutely destroys your computers RAM by itself; no need for you to add to make things worse. I often see a PHP code written like this: 

$files=array('file1.txt','file2.txt','file3.txt');

foreach($files a $file){
	$Parser= newParser();
	$file_contents=$parser->parse_file($file);
}

Do you see the issue here? A new class is being instantiated upon every loop, this means a ton of new object are created and stored in memory. Make sure to pull that instantiated out of the loop and only call the method you need. 


4. Using isset() all over the place

I am guilty of this, but  all new programmers love  isset() and often will use it for boolean checks. The reason for this is because most new programmers work the $_GET variables and these usually require a isset($_GET['id']) to determine if the variable was actually passed, otherwise PHP will throw a warning. Newer programmers get in the habit of using isset() and just run with it. 

I often see stuff like:

$contents=file_get_contents('file_name.txt');

if(isset($contents)){
	do_watever_with($contents);
}

In this scenario that condition always defaults to true even if that file is not read or does not have any contents. if($contents) would work properly here.


3. Not using  arrays and loops

Often newer programmers don't feel comfortable with using loops so they avoid them and use other alternative which causes ugly looking and hard to maintain code. 

$user1=some_function(1);
$user2=some_function(2);
$user3=some_function(3);

//a better approach is using arrays
$users=array(1,2,3);
foreach($usersas$key=>$user){
	$users[$key]=some_function($user);
}

2. "Fixing" errors by using the "@" symbol

You should almost never need to use the "@ "error suppression, this often means you have structured your code poorly. See what the error is and try to thing of another way to stop the error from throwing.  Alternatively if you really don't feel like thinking just change your error reporting setting in your php.ini file, you should have access to php.ini with almost all the best php hosting providers .

1. Spaghetti Code 

By far the most common newb move is to mix your PHP business logic with html. To be fair PHP was developed so we can easily mix it with html. However there is a big difference  between displaying PHP variables within your html and having PHP logic or data access mixed in your. As a rule of thumb you should always separate your html display(view) and your business logic into separate files. 

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}