[Tex/LaTex] good definition for highlighting PHP code in listings

highlightinglistingssourcecode

Does someone have a good definition for highlighting PHP code with the listings package?

Best Answer

listings provides lots of options to color/customize the format of the style to your likings. Of course you should choose php as a language.

This may be a starting point:

\documentclass{article}
\usepackage{listings,xcolor}
\usepackage{inconsolata}

\definecolor{dkgreen}{rgb}{0,.6,0}
\definecolor{dkblue}{rgb}{0,0,.6}
\definecolor{dkyellow}{cmyk}{0,0,.8,.3}

\lstset{
  language        = php,
  basicstyle      = \small\ttfamily,
  keywordstyle    = \color{dkblue},
  stringstyle     = \color{red},
  identifierstyle = \color{dkgreen},
  commentstyle    = \color{gray},
  emph            =[1]{php},
  emphstyle       =[1]\color{black},
  emph            =[2]{if,and,or,else},
  emphstyle       =[2]\color{dkyellow}}

\begin{document}

\begin{lstlisting}
<?php
/* this is a stupid example */
$username = $_POST["username"];
$passwort = $_POST["passwort"];

$pass = md5($passwort);

// another comment
if($username=="Andavos" and
$pass=="fd0d9cdefd5d42dfa36c74a449aa8214")
   {
   echo "Herzlich Willkommen";
   }
else
   {
   echo "Login Fehlgeschlagen";
   }
?>
\end{lstlisting}

\end{document}

enter image description here