Trucuri Css

download Trucuri Css

of 30

Transcript of Trucuri Css

  • 8/13/2019 Trucuri Css

    1/30

    How can I control the width of a label tag?

    R:

    Label is an inline element, it cannot have width value; in order to do this you need to put display:blockorfloat:left.Using CSS, of course...label { display: block; width: 100px; }

    The widthattribute is deprecated, and CSS should always be used to control these kinds of presentationalstyles.using the inline-block is better because it doesn't force the remaining elements and/or controls to be drawn in anew line.label {

    width:200px;

    display: inline-block;

    }

    Also you may set the position, for example to absolute, and width will be applied to labelelement

    HOW TO STYLE CSS RADIOBefore and after:

    Our HTML code:

    http://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-taghttp://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-tag
  • 8/13/2019 Trucuri Css

    2/30

    label 1

    label 2

    label 3

    label 4

    We only have to add two empty span before and after the radio button. Here is the commented CSS code (only the

    essential to do the trick, without the eye-candy):

    span{

    position:absolute;

    right:0;

    width:25px;

    height:25px;

    line-height:25px;

    padding:3px;

    color:#FFF;

    text-align:center;

    background:#c06f59;

    }

    span:after{

    content:"no";/*if CSS are disbled span elements are not displayed*/

    }

    input{

    position:absolute;right:0;

    margin:0;

    width:31px;

    height:31px;

    /*hide the radio button*/

    filter:alpha(opacity=0);

    -moz-opacity:0;

    -khtml-opacity:0;

    opacity:0;

    cursor:pointer;

    }

    input[type="radio"]+span{/*the span element that immediately follow the radio button */visibility:hidden;/*temporarily hide the "YES" label*/

    background:#6EB558;

    }

    input[type="radio"]+span:after{

    content:"yes";/*if CSS are disbled span elements are not displayed*/

    }

  • 8/13/2019 Trucuri Css

    3/30

    input[type="radio"]:checked +span{

    visibility:visible;/*show the "YES" label only if the radio button is checked*/

    }

    Horizontal lists - all steps combined

    Make a basic listStart with a basic unordered list. The list items are all active (wrapped in ) which isessential for this list to work. Some CSS rules are written exclusively for the "a" element within the listitems. For this example a "#" is used as a dummy link.

    Step 2 - Remove the bullets

    To remove the HTML list bullets, set the "list-style-type" to "none".

    #navcontainer ul { list-style-type: none; }

    Step 3 - Remove padding and margins

    Standard HTML lists have a certain amount of left-indentation. The amount varies on each browser. Somebrowsers use padding (Mozilla, Netscape, Safari) and others use margins (Internet Explorer, Opera) to setthe amount of indentation.

    To remove this left-indentation consistently across all browsers, set both padding and margins to "0" for the"UL".

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

    Step 4 - Display inlineTo force the list into one line, apply "display: inline;" to the "LI".

    CSS CODE

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

  • 8/13/2019 Trucuri Css

    4/30

    #navcontainer ul li { display: inline; }

    Step 5 - Removing text decoration

    At this point you may wish to remove the text underline. It is a common practise for navigation not to haveunderlines as their placement and other feedback mechanisms make them more obviously links. However,you should be aware that modifying standard hyperlink behaviour (such as underlines) can be confusing forsome users, who may not realise that the item is a link.

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

    #navcontainer ul li { display: inline; }

    #navcontainer ul li a { text-decoration: none; }

    Step 6 - Padding around list items

    To make each list item into a box, we need to add padding to the "a" element.

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

    #navcontainer ul li { display: inline; }

    #navcontainer ul li a

    {

    text-decoration: none;

    padding: .2em 1em;

    }

    Step 7 - Adding background color

    At this point a background color and border can be applied. There are many combinations of border andbackground colors that can be used.

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

    #navcontainer ul li { display: inline; }

  • 8/13/2019 Trucuri Css

    5/30

    #navcontainer ul li a

    {

    text-decoration: none;

    padding: .2em 1em;

    color: #fff;

    background-color: #036;

    }

    Step 8 - Add a rollover color

    Use "a:hover" to set a second background color, as a rollover. Roll over the list now you will see how itworks.

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;

    }

    #navcontainer ul li { display: inline; }

    #navcontainer ul li a

    {

    text-decoration: none;

    padding: .2em 1em;

    color: #fff;

    background-color: #036;

    }

    #navcontainer ul li a:hover

    {color: #fff;

    background-color: #369;

    }

    Step 9 - Center the list

    #navcontainer ul

    {

    margin: 0;

    padding: 0;

    list-style-type: none;text-align: center;

    }

    #navcontainer ul li { display: inline; }

    #navcontainer ul li a

    {

    text-decoration: none;

    padding: .2em 1em;

  • 8/13/2019 Trucuri Css

    6/30

    color: #fff;

    background-color: #036;

    }

    #navcontainer ul li a:hover

    {

    color: #fff;

    background-color: #369;

    }

    the "display" property

    display is CSS's most important property for controlling layout. Every element has a default display value

    depending on what type of element it is. The default for most elements is usually block or inline. A block

    element is often called a block-level element. An inline element is always just called an inline element.

    block

    div is the standard block-level element. A block-level element starts on a new line and stretches out to the

    left and right as far as it can. Other common block-level elements are p and form, and new in HTML5

    are header, footer, section, and more.

    inline

    span is the standard inline element. An inline element can wrap some text inside a paragraph like

    this without disrupting the flow of that paragraph. Thea element is the most common inline

    element, since you use them for links.

    none

    Another common display value is none. Some specialized elements such as scriptuse this as their default.

    It is commonly used with JavaScript to hide and show elements without really deleting and recreating them.

    This is different from visibility. Setting display to none will render the page as though the element does notexist. visibility: hidden; will hide the element, but the element will still take up the space it would if it was

    fully visible.

    other display values

    There are plenty of more exotic display values, such as list-item and table.Here is an exhaustive list.We'll

    discuss inline-block and flex later on.

    https://developer.mozilla.org/en-US/docs/CSS/displayhttps://developer.mozilla.org/en-US/docs/CSS/displayhttps://developer.mozilla.org/en-US/docs/CSS/displayhttps://developer.mozilla.org/en-US/docs/CSS/display
  • 8/13/2019 Trucuri Css

    7/30

    extra credit

    As I mentioned, every element has a default display type. However, you can alwaysoverride this! Though it

    wouldn't make sense to make an inline div, you can use this to customize the display of elements that have

    particular semantics. A common example is making inline li elements for horizontal menus.

    SS Inline Elements

    Inline elements are distinct from block level elements. Whereas block-level elements modify theflow of any text around them, inline elements are designed to flow with the text. Also, inline

    elements do not have a line break before and after. Properties of inline elements:

    they flow with text.

    they have line-height.

    the element height and width is dependent on the content and will not collapse to the

    borders of the parent element.

    So what does all this mean?! It can be displayed in the following way:

    Green Line - the content areaRed Line - the border. The distance between border and content area is the paddingarea.Blue Line - the element edge. The distance between border and element edge is the margin.

    Block vs Inline display style in CSS HTML elements can be

    displayed either in block or inline style.

    The difference between these is one of the most basic things you need to know in order to use

    CSS effectively.

    The 3 ways that HTML elements can be displayed All HTML elements are naturally displayed in oneof the following ways:

    Block: Takes up the full width available, with a new line before and after (display:block;)

    Inline :Takes up only as much width as it needs, and does not force new lines (display:inline;)Not displayed: Some tags are not visible (display:none;)

    Block example

    tags and tags are naturally displayed block-style.

    (I say "naturally" because you can override the display style by setting the CSS display

    property e.g. display:inline;.)

    A block-display element will span the full width of the space available to it, and so will

    start on a new line in the flow of HTML. The flow will continue on a new line after the

    block-display element.

    Here I've started a paragraph and now I'm going to insert a

    new div inside my paragraph

    and then continue the text here...

    See how the jumped in and took over the full width of the space?

    Common HTML elements that are naturally block-display include:

  • 8/13/2019 Trucuri Css

    8/30

    Your general-purpose box

    ... All headings

    Paragraph

    , , Lists (unordered, ordered and definition)

    , ,

    List items, definition list terms, and definition list definitions

    Tables

    Like an indented paragraph, meant for quoting passages of text

    Indicates a block of preformatted code

    An input form

    Inline example Inline-display elements don't break the flow. They just fit in with the flow of thedocument. So here I've got a paragraph going on, and I'm going to add a tag that has a

    yellow background and italic text. See how it just fits right in with the flow of the text? More

    elements are naturally inline-style, including:

    Your all-purpose inline element

    Anchor, used for links (and also to mark specific targets on a page for direct linking)

    Used to make your content strong, displayed as bold in most browsers, replaces the

    narrower (bold) tag

    Adds emphasis, but less strong than . Usually displayed as italic text, and replaces

    the old (italic) tagImage
    The line-break is an odd case, as it's an inline element that forces a new line.

    However, as the text carries on on the next line, it's not a block-level element.Form input fields like andIndicates an abbr. (hover to see how it works)Working much like the abbreviation, but used for things like this TLA.You change the display property of any elements

    Although each HTML element has its natural display style, you can over-ride these inCSS.

    This can be very useful when you want your page to look a particular way while using

    semantically-correct HTML. Examples Say you want to provide a list of items, but you

    don't want a big bulleted list. You just want to say that you're going to the store to buy:some fizzy drinks, a chainsaw, and some nylon stockings. Or maybe you want a nice

    toolbar, which is stricly a list (of links) and so should be marked up as a

    . Here's the code

    HomeAbout usProductsFAQs

    Contact us

  • 8/13/2019 Trucuri Css

    9/30

    PHP and HTML Radio Buttons

    A Radio Button is a way to restrict users to having only one choice. Examples are :Male/Female, Yes/No, or answers to surveys and quizzes.

    Here's a simple from with just two radio buttons and a Submit button:

    Make sure you save your work as radioButton.php, as that's where we're posting the Form to itself.

    To get the value of a radio button with PHP code, again you access the NAME attribute of theHTML form elements. In the HTML above, the NAME of the Radio buttons is the same "gender". The first Radio Button has a value of "male" and the second Radio Button has avalue of female. When you're writing your PHP code, it's these values that are returned.

    Here's some PHP code. Add it to the HEAD section of your HTML:

    This is more or less the same code as we used for the text box! The only thing that's changed(apart from the variable name) is the NAME of the HTML form element we want to access "gender". The last line just prints the value to the page. Again, though, we can add code todetect if the user clicked the Submit button:

    if (isset($_POST['Submit1'])) {$selected_radio = $_POST['gender'];print $selected_radio;

    }

    Again, this is the same code you saw earlierjust access the form element called 'Submit1'and see if it is set. The code only executes if it is.

    Try out the code. Select a radio button and click Submit button. The choice you made isprinted to the page - either "male" or "female". What you will notice, however, when you tryout the code is that the dot disappears from your selected radio button after the Submit isclicked. Again, PHP is not retaining the value you selected. The solution for radio Buttons,though, is a little more complex than for text boxes

    Radio buttons have another attribute - checked or unchecked. You need to set which buttonwas selected by the user, so you have to write PHP code inside the HTML with thesevalues - checked or unchecked.Here's one way to do it:

    The PHP code:

  • 8/13/2019 Trucuri Css

    10/30

    $female_status = 'unchecked';

    if (isset($_POST['Submit1'])) {

    $selected_radio = $_POST['gender'];

    if ($selected_radio = = 'male')

    {

    $male_status = 'checked';

    }else if ($selected_radio = = 'female')

    {

    $female_status = 'checked';

    }}

    ?>

    The HTML FORM code:

    >Female

    Did we say a little more complex? OK, it's much more complex than any code you've writtenso far! Have a look at the PHP code inside the HTML first:

    This is just a print statement. What is printed out is the value inside of the variable. What isinside of the variable will be either the word "checked" or the word "unchecked". Which it isdepends on the logic from our long PHP at the top of the page. Let's break that down.

    First we have two variables at the top of the code:

    $male_status = 'unchecked';$female_status = 'unchecked';

    These both get set to unchecked. That's just in case the page is refreshed, rather than theSubmit button being clicked.

    Next we have our check to see if Submit is clicked:

    if (isset($_POST['Submit1'])) {

    }

  • 8/13/2019 Trucuri Css

    11/30

    Exactly the same as before. As is the next line that puts which radio button was selected intothe variable:

    $selected_radio = $_POST['gender'];

    We then need some conditional logic. We need to set a variable to "checked", so we havean if, else ifconstruction:

    if ($selected_radio == 'male'){}else if ($selected_radio == 'female')

    {

    }

    All we're doing is testing what is inside of the variable called $selected_radio. If it's 'male' doone thing; if it's 'female', do another. But look at what we're doing:

    if ($selected_radio == 'male')

    {

    $male_status = 'checked';

    }else if ($selected_radio = = 'female') {

    $female_status = 'checked';

    }

    If the 'male' button was clicked then set the $male_statusvariable to a value of 'checked'. Ifthe 'female' option button was clicked then set the $female_statusvariable to a value of'checked'.

    So the code works because of the values inside of twovariables: $male_statusand$female_status.

    Yes, the code is very messy but radio Buttons can be a quite tricky, when you want to retainthe value of the selected item. Speaking of tricky checkboxes are up next!

    PHP and HTML Checkboxes

    LikeRadio buttons,checkboxes are used to give visitors a choice of options. Whereas Radio Buttonsrestrict users to only one choice, you can select more than one option with Checkboxes.

    Here's a page that asks users to choose which course books they want to order:

    As you can see, five items can be selected. Only three are chosen at the moment. When the button isclicked you, as the programmer, want to do at least two things: record which checkboxes were ticked,and have PHP "remember" which items were chosen, just in case of errors.

    You don't want the ticks disappearing from the checkboxes, if the user has failed to enter some otherdetails incorrectly. We saw with Radio Buttons that this can involve some tricky coding. The same istrue for checkboxes. Let's have a look at one solution to the problem.

    Because the code is a little more complex, we've included it inthe files you downloaded.The scriptyou're looking for is checkboxes.php, and is in the scriptsfolder. Open it up and take a look at the

    http://www.homeandlearn.co.uk/php/php4p10.htmlhttp://www.homeandlearn.co.uk/downloads.htmlhttp://www.homeandlearn.co.uk/downloads.htmlhttp://www.homeandlearn.co.uk/php/php4p10.html
  • 8/13/2019 Trucuri Css

    12/30

    code. Here it is in full, if you want to copy and paste it:

    The Checkboxes Script

    Note one thing about the HTML checkbox elements: they all have different NAME values (ch1, ch2ch3, etc). When we coded for the Radio Buttons, we gave the buttons the same NAME. That'sbecause only one option can be selected with Radio Buttons. Because the user can select more thanone option with Checkboxes, it makes sense to give them different NAME values, and treat them as

    separate entities (but some advocate treating them just like Radio Buttons).In your PHP code, the technique is to check whether each checkbox element has been checked ornot. It's more or less the same as for the radio Buttons. First we set up five variable and set them allthe unchecked, just like we did before:

    $ch1 = 'unchecked';$ch2 = 'unchecked';$ch3 = 'unchecked';$ch4 = 'unchecked';$ch5 = 'unchecked';

    The next thing is the same as well: check to see if the Submit button was clicked:

    if (isset($_POST['Submit1'])) {}

    Inside of this code, however, we have another isset( )function:

    if ( isset($_POST['ch1']) ) {

    }

    This time, we're checking to see if a checkbox was set. We need to do this because of a peculiarity ofHTML checkboxes. If they are not ticked, they have no value at all, so nothing is returned! If you trythe code without checking if the checkboxes are set, then you'll have to deal with a lot of "Undefined"errors.

    If the checkbox is ticked, though, it will return a value. And so the isset( ) function will be true. If theisset( ) function is true, then our code inside of the if statement gets executed:

    if ($ch1 == 'net') {

    $ch1 = 'checked';

    }

    This is yet another If Statement! But we're just checking the value of a variable. We need to know whatis inside of it. This one says, "If the value inside of the variable called $ch1is 'net' then execute somecode.

    The code we need to execute is to put the text 'checked' inside of the variable called $ch1. The restof the if statements are the same one for each checkbox on the form.

    The last thing we need to do is to print the value of the variable to the HTML form:

    So we're just printing what is inside of the variable called $ch1. This will either be "unchecked" or"checked",

    http://www.homeandlearn.co.uk/php/checkBoxScript.htmhttp://www.homeandlearn.co.uk/php/checkBoxScript.htm
  • 8/13/2019 Trucuri Css

    13/30

    There are other solution for checkboxes, but none seem simple! The point here, though, is that to getthe job done we used Conditional Logic.

    How to validate checkboxes using JavaScript

    Another way to deal with checkboxes, though, is with some JavaScript. The following script was sentto us by Tapan Bhanot. It uses JavaScript to validate the checkboxes before sending it to a PHP script.Note how the checkboxes all have the same name on the HTML form, and that it is being posted to a

    PHP script called step2.php:

    PHP Submit buttons

    In theprevious section,you saw how to get text from a textbox when a Submit button on a form wasclicked. However, when you first load the page the text still displays.

    The reason why the text displays when the page is first loaded is because the script executes whetherthe button is clicked or not. This is the problem you face when a PHP script is on the same page asthe HTML, and is being submitted to itself in the ACTION attribute.

    To get round this, you can do a simple check using another IF Statement. What you do is to check ifthe Submit button was clicked. If it was, then run your code. To check if a submit button was clicked,

    use this:if ( isset($_POST['Submit1'] ) ){ }

    Now that looks a bit messy! But it actually consists of three parts:

    if ( ) { }isset( )$_POST['Submit1']

    You know about the if statement. But in between the round brackets, we have isset( ). This is aninbuilt function that checks if a variable has been set or not. In between the round brackets, you typewhat you want isset( ) to check. For us, this is $_POST['Submit']. If the user just refreshed the page,then no value will be set for the Submit button. If the user did click the Submit button, then PHP willautomatically return a value. Change you script from theprevious page to the following and try it out:

    if (isset($_POST['Submit1'])) {

    $username = $_POST['username'];

    if ($username == "letmein") {

    print ("Welcome back, friend!");}else

    {print ("You're not a member of this site");}

    }

    Make a note of where all those messy round, square and curly brackets are. Miss one out and you'll

    get an error!

    In the next part, you'll see how to submit your form data to a PHP script on a different page.

    How to avoid resending data on refresh in phpI have a page "index.php" where i have a link called "add_users.php".In "add_users.php",i accept userinformation and come back to the same page "index.php" where information comes through post action and getsinserted into the database.When i refresh the page orhit back button,resend box appears.I went through many

    http://www.homeandlearn.co.uk/php/php4p6.htmlhttp://www.homeandlearn.co.uk/php/php4p6.htmlhttp://stackoverflow.com/questions/2666882/how-to-avoid-resending-data-on-refresh-in-phphttp://stackoverflow.com/questions/2666882/how-to-avoid-resending-data-on-refresh-in-phphttp://stackoverflow.com/questions/2666882/how-to-avoid-resending-data-on-refresh-in-phphttp://www.homeandlearn.co.uk/php/php4p6.htmlhttp://www.homeandlearn.co.uk/php/php4p6.html
  • 8/13/2019 Trucuri Css

    14/30

    solution where they asked me to create third page.I tried doing that as follows:After inserting values in database,Iredirected ht page as header('Location:http://mysite.com/thankyou.php, and in thankyou.php I again redirectedthe page to index.php.But getting warning as Cannot modify header information - headers already sent by....provide me a better solution. Thank You in advance.

    Exceptions

    Table of Contents Extending Exceptions

    PHP 5 has an exception model similar to that of other programming languages. An exceptioncan be thrown, and caught ("catched") within PHP. Code may be surrounded in a tryblock,

    to facilitate the catching of potential exceptions. Each trymust have at least onecorresponding catchblock. Multiple catchblocks can be used to catch different classes ofexceptions. Normal execution (when no exception is thrown within the tryblock, or when

    a catchmatching the thrown exception's class is not present) will continue after thatlast catchblock defined in sequence. Exceptions can be thrown (or re-thrown) withina catchblock.

    When an exception is thrown, code following the statement will not be executed, and PHP willattempt to find the first matching catchblock. If an exception is not caught, a PHP FatalError will be issued with an "Uncaught Exception ..." message, unless a handler has beendefined withset_exception_handler().

    In PHP 5.5 and later, a finallyblock may also be specified after the catchblocks. Codewithin the finallyblock will always be executed after the tryand catchblocks, regardless of

    whether an exception has been thrown, and before normal execution resumes.

    The thrown object must be an instance of theExceptionclass or a subclass ofException.Trying to throw an object that is not will result in a PHP Fatal Error.

    Note:

    Internal PHP functions mainly useError reporting,only modernObject

    orientedextensions use exceptions. However, errors can be simply translated toexceptions withErrorException.

    Tip

    TheStandard PHP Library (SPL)provides a good number ofbuilt-in exceptions.

    Example #1 Throwing an Exception

  • 8/13/2019 Trucuri Css

    15/30

    return 1/$x;}

    try {echo inverse(5) . "\n";echo inverse(0) . "\n";

    } catch (Exception $e) {

    echo 'Caught exception: ', $e->getMessage(), "\n";}

    // Continue executionecho "Hello World\n";?>

    The above example will output:

    0.2

    Caught exception: Division by zero.

    Hello World

    Example #2 Exception handling with a finallyblock

    The above example will output:

    0.2

  • 8/13/2019 Trucuri Css

    16/30

    First finally.

    Caught exception: Division by zero.

    Second finally.

    Hello World

    Example #3 Nested Exception

    The above example will output:

    string(4) "foo!"

    Extending ExceptionsFAQ: things you need to know about namespaces

    [edit]Last updated: Fri, 15 Nov 2013

    add a noteUser Contributed NotesExceptions - [20notes]

    updown

    5

    sander at rotorsolutions dot nl4 months ago

    http://us2.php.net/manual/en/language.exceptions.extending.phphttp://us2.php.net/manual/en/language.namespaces.faq.phphttp://us2.php.net/manual/en/language.namespaces.faq.phphttps://edit.php.net/?project=PHP&perm=en/language.exceptions.phphttps://edit.php.net/?project=PHP&perm=en/language.exceptions.phphttps://edit.php.net/?project=PHP&perm=en/language.exceptions.phphttp://us2.php.net/manual/add-note.php?sect=language.exceptions&redirect=http://us2.php.net/manual/en/language.exceptions.phphttp://us2.php.net/manual/vote-note.php?id=112527&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=112527&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#112527http://us2.php.net/manual/en/language.exceptions.php#112527http://us2.php.net/manual/en/language.exceptions.php#112527http://us2.php.net/manual/vote-note.php?id=112527&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=112527&page=language.exceptions&vote=uphttp://us2.php.net/manual/add-note.php?sect=language.exceptions&redirect=http://us2.php.net/manual/en/language.exceptions.phphttp://us2.php.net/manual/add-note.php?sect=language.exceptions&redirect=http://us2.php.net/manual/en/language.exceptions.phphttp://us2.php.net/manual/en/language.exceptions.phphttps://edit.php.net/?project=PHP&perm=en/language.exceptions.phphttp://us2.php.net/manual/en/language.namespaces.faq.phphttp://us2.php.net/manual/en/language.exceptions.extending.phphttp://us2.php.net/manual/en/language.exceptions.extending.phphttp://us2.php.net/manual/en/language.exceptions.extending.php
  • 8/13/2019 Trucuri Css

    17/30

    Just an example why finally blocks are usefull (5.5)

    up

    down

    23

    Johan

    2 years ago

    Custom error handling on entire pages can avoid half rendered pages for theusers:

  • 8/13/2019 Trucuri Css

    18/30

    ?>

    updown

    9

    michael dot ochs at gmx dot net5 years ago

    Actually it isn't possible to do:

    This leads to a T_THROW Syntax Error. If you want to use this kind of

    exceptions, you can do the following:

    updown

    4

    alex dowgailenko [at] g mail . com2 years ago

    If you use the set_error_handler() to throw exceptions of errors, you may

    encounter issues with __autoload() functionality saying that your classdoesn't exist and that's it.

    If you do this:

  • 8/13/2019 Trucuri Css

    19/30

    $this->helloWorld();

    } catch (MyException $e) {

    throw new Exception('Problem in foobar',0,$e);}

    }

    protected function helloWorld(){throw new MyException('Problem in helloWorld()');

    }}

    $tester = new Tester;

    try{

    $tester->foobar();} catch (Exception $e) {

    echo $e->getTraceAsString();}

    ?>

    The trace will only show $tester->foobar() and not the call made to$tester->helloWorld().

    In other words, if you pass a previous exception to a new one, the previousexception's stack trace is taken into account in the new exception.

    up

    down

    1

    jon at hackcraft dot net6 years ago

    Further to dexen at google dot me dot up with "use destructors to perform acleanup in case of exception". The fact that PHP5 has destructors,

    exception handling, and predictable garbage collection (if there's a singlereference in scope and the scope is left then the destructor is called

    immediately) allows for the use of the RAII idiom.

    http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initializationand myown http://www.hackcraft.net/RAII/describe this.

    up

    down

    4

    ask at nilpo dot com4 years ago

    http://us2.php.net/manual/vote-note.php?id=72567&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=72567&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#72567http://us2.php.net/manual/en/language.exceptions.php#72567http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initializationhttp://www.hackcraft.net/RAII/http://us2.php.net/manual/vote-note.php?id=91159&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=91159&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#91159http://us2.php.net/manual/en/language.exceptions.php#91159http://us2.php.net/manual/en/language.exceptions.php#91159http://us2.php.net/manual/vote-note.php?id=91159&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=91159&page=language.exceptions&vote=uphttp://www.hackcraft.net/RAII/http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initializationhttp://us2.php.net/manual/en/language.exceptions.php#72567http://us2.php.net/manual/vote-note.php?id=72567&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=72567&page=language.exceptions&vote=up
  • 8/13/2019 Trucuri Css

    20/30

    If you intend on creating a lot of custom exceptions, you may find this

    code useful. I've created an interface and an abstract exception class

    that ensures that all parts of the built-in Exception class are preservedin child classes. It also properly pushes all information back to the

    parent constructor ensuring that nothing is lost. This allows you toquickly create new exceptions on the fly. It also overrides the default

    __toString method with a more thorough one.

  • 8/13/2019 Trucuri Css

    21/30

    public function __toString()

    {

    return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n"

    . "{$this->getTraceAsString()}";}

    }?>

    Now you can create new exceptions in one line:

    Here's a test that shows that all information is properly preservedthroughout the backtrace.

  • 8/13/2019 Trucuri Css

    22/30

    3

    Shot (Piotr Szotkowski)

    5 years ago

    Normal execution (when no exception is thrown within the try block, *or

    when a catch matching the thrown exceptions class is not present*) willcontinue after that last catch block defined in sequence.

    If an exception is not caught, a PHP Fatal Error will be issued with an

    Uncaught Exception message, unless a handler has been defined with

    set_exception_handler().

    These two sentences seem a bit contradicting about what happens when acatch matching the thrown exceptions class is not present (and the second

    sentence is actually correct).

    up

    down1

    Edu

    4 months ago

    The "finally" block can change the exception that has been throw by thecatch block.

    The output is:

    Hello catch inHello finally

    Bye catch out

    up

    http://us2.php.net/manual/en/language.exceptions.php#86476http://us2.php.net/manual/en/language.exceptions.php#86476http://us2.php.net/manual/vote-note.php?id=112507&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=112507&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#112507http://us2.php.net/manual/en/language.exceptions.php#112507http://us2.php.net/manual/vote-note.php?id=106696&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=106696&page=language.exceptions&vote=uphttp://us2.php.net/manual/en/language.exceptions.php#112507http://us2.php.net/manual/vote-note.php?id=112507&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=112507&page=language.exceptions&vote=uphttp://us2.php.net/manual/en/language.exceptions.php#86476
  • 8/13/2019 Trucuri Css

    23/30

    down

    1

    Sawsan

    1 year ago

    the following is an example of a re-thrown exception and the using of

    getPrevious function:

    http://us2.php.net/manual/vote-note.php?id=106696&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#106696http://us2.php.net/manual/en/language.exceptions.php#106696http://us2.php.net/manual/en/language.exceptions.php#106696http://us2.php.net/manual/vote-note.php?id=106696&page=language.exceptions&vote=down
  • 8/13/2019 Trucuri Css

    24/30

    up

    down

    1

    zmunoz at gmail dot com

    3 years ago

    When catching an exception inside a namespace it is important that you

    escape to the global space:

    updown

    1

    jazfresh at hotmail.com7 years ago

    Sometimes you want a single catch() to catch multiple types of Exception.

    In a language like Python, you can specify multiple types in a catch(), butin PHP you can only specify one. This can be annoying when you want handle

    many different Exceptions with the same catch() block.

    However, you can replicate the functionality somewhat, becausecatch( $var) will match the given *or any of it's

    sub-classes*.

    For example:

  • 8/13/2019 Trucuri Css

    25/30

    try {

    if(!is_readable($somefile))

    throw new IOError("File is not readable!");if(!user_has_access_to_file($someuser, $somefile))

    throw new AccessControl("Permission denied!");if(!display_file($somefile))

    throw new DisplayException("Couldn't display file!");

    } catch (FileException $e) {

    // This block will catch FileException, AccessControl or IOErrorexceptions, but not Exceptions or DisplayExceptions.

    echo "File error: ".$e->getMessage();

    exit(1);

    }?>

    Corollary: If you want to catch *any* exception, no matter what the type,

    just use "catch(Exception $var)", because all exceptions are sub-classes ofthe built-in Exception.

    up

    down

    0

    chugadie dot geo at yahoo dot com5 years ago

    @webmaster at asylum-et dot com

    What Mo is describing is bug 44053 (http://bugs.php.net/bug.php?id=44053)in which exceptions cannot be caught if you are using a custom error

    handler to catch warnings, notices, etc.

    up

    down

    0

    omnibus at omnibus dot edu dot pl5 years ago

    Just to be more precise in what Frank found:

    Catch the exceptions always in order from the bottom to the top of theException and subclasses class hierarchy. If you have class MyException

    extending Exception and class My2Exception extending MyException alwayscatch My2Exception before MyException.

    Hope this helps

    updown

    http://us2.php.net/manual/vote-note.php?id=81603&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=81603&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#81603http://us2.php.net/manual/en/language.exceptions.php#81603http://bugs.php.net/bug.php?id=44053http://us2.php.net/manual/vote-note.php?id=79577&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=79577&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#79577http://us2.php.net/manual/en/language.exceptions.php#79577http://us2.php.net/manual/vote-note.php?id=78591&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=78591&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=78591&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=78591&page=language.exceptions&vote=uphttp://us2.php.net/manual/en/language.exceptions.php#79577http://us2.php.net/manual/vote-note.php?id=79577&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=79577&page=language.exceptions&vote=uphttp://bugs.php.net/bug.php?id=44053http://us2.php.net/manual/en/language.exceptions.php#81603http://us2.php.net/manual/vote-note.php?id=81603&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=81603&page=language.exceptions&vote=up
  • 8/13/2019 Trucuri Css

    26/30

    0

    hartym dot dont dot like dot spam at gmail dot com

    6 years ago

    @serenity: of course you need to throw exception within the try block,

    catch will not watch fatal errors, nor less important errors but onlyexceptions that are instanceof the exception type you're giving. Of course

    by within the try block, i mean within every functions call happening intry block.

    For example, to nicely handle old mysql errors, you can do something like

    this:

    By doing so, you're aiming at the don't repeat yourself (D.R.Y) concept, by

    managing error handling at only one place for the whole.

    updown

    0

    fjoggen at gmail dot com

    7 years ago

    This code will turn php errors into exceptions:

  • 8/13/2019 Trucuri Css

    27/30

    function exceptions_error_handler($severity, $message, $filename, $lineno)

    {

    throw new ErrorException($message, 0, $severity, $filename, $lineno);}

    set_error_handler('exceptions_error_handler');

    ?>

    However since doesn't work with fatal errors,

    you will not be able to throw them as Exceptions.

    updown

    -3

    mike at clove dot com3 years ago

    The PHP documentation has gone from very useful to hideously obstructive.

    The people who are rearranging the doc into little, tiny chunks which are

    hyperlinked all over the place obviously never write code.

    I just spent 10 minutes trying to find the name of an IO Exception so I canuse it in some code I'm writing.

    Old Doc: I would go to the index, click on Exceptions and then scroll down

    the page (or do a find on IO) and there it would be. 10 seconds tops.

    New Doc: Go to the index click on Predefined ExceptionsClick on Exception - find description of Exception Object - info not there

    Back ButtonClick on Error Exception - find description of Generic ErrorExeption

    object

    Back ButtonClick on SPL Exceptions (what the hell is this? - something new?)

    Look at Table of contents: 13 Exception Categories - none of whichlooks like an IOException

    Click on Predefined Exceptions in the See Also -Back to Previous Useless Page

    First You completely screw up the Perl Regular Expression page by chopping

    it into tiny, obscure chunks and now you destroy the exceptiondocumentation.

    PLEASE put it back the way it was.

    Or get somebody who actually uses this stuff like a handbook while writingcode to fix it

    http://us2.php.net/manual/vote-note.php?id=97255&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=97255&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#97255http://us2.php.net/manual/en/language.exceptions.php#97255http://us2.php.net/manual/en/language.exceptions.php#97255http://us2.php.net/manual/vote-note.php?id=97255&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=97255&page=language.exceptions&vote=up
  • 8/13/2019 Trucuri Css

    28/30

    Or shoot somebody.

    Incredibly frustrated and thinking of rewriting everything in Python,

    Mike Howard

    updown

    0

    jim at anderos dot com

    1 month ago

    If you are using a namespace, you must indicate the global namespace whenusing Exceptions.

    up

    down

    0

    sander at rotorsolutions dot nl4 months ago

    Just an example why finally blocks are usefull (5.5)

  • 8/13/2019 Trucuri Css

    29/30

    }

    }

    function example2() {

    try {//open sql connection check user as example

    if(condition) {return false;}

    }finally {

    //close the sql connection, this will be executed even if the return is

    called.

    }}

    up

    down0

    sander at rotorsolutions dot nl4 months ago

    Just an example why finally blocks are usefull (5.5)

  • 8/13/2019 Trucuri Css

    30/30

    ?>

    up

    down

    -1

    cyrus+php at boadway dot ca1 month ago

    There's some inconsistent behaviour associated with PHP 5.5.3's finally andreturn statements. If a method returns a variable in a try block (e.g.

    return $foo;), and finally modifies that variable, the /modified/ value isreturned. However, if the try block has a return that has to be evaluatedin-line (e.g. return $foo+0;), finally's changes to $foo will /not/ affect

    the return value.

    [code]

    function returnVariable(){$foo = 1;

    try{

    return $foo;} finally {

    $foo++;}

    }

    function returnVariablePlusZero(){$foo = 1;

    try{return $foo + 0;

    } finally {$foo++;

    }

    }

    $test1 = returnVariable(); // returns 2, not the correct value of 1.$test2 = returnVariablePlusZero(); // returns correct value of 1, but

    inconsistent with $test1.[/code]

    It looks like it's trying to be efficient by not allocating additional

    memory for the return value when it thinks it doesn't have to, but the specis that finally is run after try is completed execution, and that includes

    the evaluation of the return expression.

    One could argue (weakly) that the first method should be the correctresult, but at least the two methods should be consistent.

    http://us2.php.net/manual/vote-note.php?id=113368&page=language.exceptions&vote=uphttp://us2.php.net/manual/vote-note.php?id=113368&page=language.exceptions&vote=downhttp://us2.php.net/manual/en/language.exceptions.php#113368http://us2.php.net/manual/en/language.exceptions.php#113368http://us2.php.net/manual/en/language.exceptions.php#113368http://us2.php.net/manual/vote-note.php?id=113368&page=language.exceptions&vote=downhttp://us2.php.net/manual/vote-note.php?id=113368&page=language.exceptions&vote=up