Your first_draft() and forth_draft() produce differing results when an attribute is missing from $markup.
At this point, the exercise appears as one of futility. We don't know what the problem is, which parameters are global, which are variable. We don't know what the inputs are to the function, or even if it's a function that you need in the first place.
Please concentrate on the correctness before tackling the overhead. Please expound on the actual problem before coding up a solution. Maybe we can comment on that and offer alternatives.